2017-10-12 146 views
1

php.net中有关url的所有示例都只有使用http而不使用https进行了演示。我真的不明白为什么没有用https来证明这一点。 file_get_contents对于http URL返回true,对于https URL为false返回false。带有https的PHP file_get_contents参数返回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.上述代码已被集成为每在计算器给出的解决方案,其中没有在结果而改变。 2.启用openssl并启用allow_url_fopen

OUTPUT我得到:

阵列(9){[0] =>串(5) “HTTPS”[1] =>串(4) “FTPS”[2] =>串(3 )“php”[3] => string(4)“file”[4] => string(4)“glob”[5] => string(4)“data”[6] => string(4) http“[7] => string(3)”ftp“[8] => string(4)”phar“}

openssl:yes http wrapper:yes https wrapper:yes wrappers:array(0 =>' https',1 =>'ftps',2 =>'php',3 =>'file',4 =>'glob',5 =>'data',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失败。错误号= 0 FILENAME.PHP第40行

布尔(假)失败

+4

确定'$ _REQUEST( 'urlHeader');'是正确的? '$ _REQUEST'不是函数 – apokryfos

+2

也许这个[link](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https)可以帮助 –

+1

[如何让文件\ _get \ _contents()与HTTPS协同工作?](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with -https) –

回答