【发布时间】:2018-03-24 13:58:32
【问题描述】:
php.net 中有关 url 的所有示例都仅使用 http 而非 https 进行了演示。我真的不明白为什么没有用 https 证明这一点。对于 http URL,file_get_contents 返回 true,对于 https 返回 false 网址。
- 我是否需要在执行此功能的服务器中启用某些功能?
- 我知道这是一个逻辑错误,但这个逻辑错误发生在一个内置函数中。我没有任何语法错误。我在这里错过了什么?
- 我的预期输出是,当一个 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