【问题标题】:PHP file_get_contents parameter with https returns false带有 https 的 PHP file_get_contents 参数返回 false
【发布时间】:2018-03-24 13:58:32
【问题描述】:

php.net 中有关 url 的所有示例都仅使用 http 而非 https 进行了演示。我真的不明白为什么没有用 https 证明这一点。对于 http URL,file_get_contents 返回 true,对于 https 返回 false 网址。

  1. 我是否需要在执行此功能的服务器中启用某些功能?
  2. 我知道这是一个逻辑错误,但这个逻辑错误发生在一个内置函数中。我没有任何语法错误。我在这里错过了什么?
  3. 我的预期输出是,当一个 https url 被传递到它时,从 file_get_contents 获得一个 true 返回。

代码:

function connectMe() {
   $urlHeader = $_REQUEST['urlHeader'];
   // if this is a http url it's working for file_get_contents returns true
   // if this is a https url it's not working file_get_contents gives false
   $status = send($urlHeader);
   var_dump($status);
   echo $status ? 'success' : 'failed';
   exit;
}

function send($url) {
    $ctx = stream_context_create(
        array(
        'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>NULL
        )
      )
    );
var_dump(stream_get_wrappers());
$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);
return file_get_contents($url, 0, $ctx);
}

注意: 1. 上面的代码已经按照stackoverflow中给出的解决方案进行了集成,结果没有任何变化。 2.openssl开启,allow_url_fopen开启。

我得到的输出:

array(9) { [0]=> string(5) "https" [1]=> string(4) "ftps" [2]=> string(3) "php" [3]=> string (4) "文件" [4]=> 字符串(4) "glob" [5]=> 字符串(4) "数据" [6]=> 字符串(4) "http" [7]=> 字符串(3 ) "ftp" [8]=> 字符串(4) "phar" }

openssl: 是 http 包装器: 是 https 包装器: 是包装器: 数组 (0 => 'https', 1 => 'ftps', 2 => 'php', 3 => 'file', 4 => ' glob', 5 => '数据', 6 => 'http', 7 => 'ftp', 8 => 'phar', )

警告:file_get_contents(): php_network_getaddresses: gethostbyname 失败。第 40 行的 FILENAME.PHP 中的 errno=0

警告:file_get_contents(https://someIPhere 或网站):无法打开流:php_network_getaddresses:gethostbyname 失败。第 40 行的 FILENAME.PHP 中的 errno=0

bool(false) 失败

【问题讨论】:

  • 你确定$_REQUEST('urlHeader'); 是正确的吗? $_REQUEST 不是函数
  • 也许这个link 可以提供帮助
  • @apokryfos 我觉得应该是$_REQUEST['urlHeader'];
  • @CommonMan 实际上,正是 SO 的工作原理。 许多 此处发布的问题是重复的。而且您没有表明您对此问题有任何基本调试。请告知我们您为解决此问题所做的工作以及这些尝试的结果。

标签: php https file-get-contents


【解决方案1】:

您可能指向的是自签名 ssl。如果是这样,那可能就是原因。检查以确保 URL 具有有效的 SSL 证书。

如果你想绕过 SSL 验证,那么你需要添加一个 stream_context_create 来设置 'verify_peer' => false。

见: file_get_contents(): SSL operation failed with code 1 (certificate verify failed)

【讨论】:

  • ...这将再次使原始问题重复:)
猜你喜欢
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 2013-07-03
  • 1970-01-01
相关资源
最近更新 更多