【问题标题】:Unable to use file_get_contents(), returns nothing无法使用 file_get_contents(),不返回任何内容
【发布时间】:2015-01-21 15:38:11
【问题描述】:

我正在尝试使用此代码从不属于我的网站获取一些数据。

<?
$text = file_get_contents("https://ninjacourses.com/explore/4/");
echo $text;
?>

但是,没有任何内容被回显,字符串长度为 0。

这个方法我以前做过,没问题,但是用这个网站,根本不行。

谢谢!

【问题讨论】:

  • 不要认为它与 HTTPS 一起工作得很好......手册说它在失败时返回 false - 会支付给 var_dump() 输出以确保。类似情况也请参见this article here
  • 您是否在php.ini 文件中启用了allow_url_fopen
  • 如果您启用 error_reporting 您将看到以下错误:SSL operation failed with code 1. OpenSSL Error messages: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
  • 我已启用allow_url_fopen。默认开启。
  • 你不能......你必须使用curl

标签: php


【解决方案1】:

我设法像这样使用curl 获取内容:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://ninjacourses.com/explore/4/");
$result = curl_exec($ch);
curl_close($ch);

【讨论】:

    【解决方案2】:

    cURL 是一种您可以从代码中点击 URL 以从中获取 html 响应的方法。 cURL 表示客户端 URL,它允许您连接其他 URL 并在您的代码中使用它们的响应

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "https://ninjacourses.com/explore/4/");
    $result = curl_exec($ch);
    curl_close($ch);
    

    我认为这对你有用 curl-with-phpanother

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2020-01-26
      • 2018-10-21
      相关资源
      最近更新 更多