file_get_content 在 php 中不适用于 https
file_get_content not working for https in php
有人对通过 https 的网站显示内容有疑问吗?该代码一直有效,直到服务器上的所有站点都获得 ssl。也许证书是 tlss 1.2?所以我试图从中执行此操作的站点现在拥有此证书。
$data = file_get_contents('https://www.ladygaga.com/');
echo $data;
Based on OpenSSL changes in PHP 5.6,试试这个:
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents("https://www.ladygaga.com/", false, stream_context_create($arrContextOptions));
echo $response;
另一种选择是这样使用 curl:
function file_get_contents_curl( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
$data = curl_exec( $ch );
curl_close( $ch );
return $data;
}
$data = file_get_contents_curl("https://www.ladygaga.com");
根据php.net
When using SSL, Microsoft IIS will violate the protocol by closing the
connection without sending a close_notify indicator. PHP will report
this as "SSL: Fatal Protocol Error" when you reach the end of the
data. To work around this, the value of error_reporting should be
lowered to a level that does not include warnings. PHP can detect
buggy IIS server software when you open the stream using the https://
wrapper and will suppress the warning. When using fsockopen() to
create an ssl:// socket, the developer is responsible for detecting
and suppressing this warning.
有人对通过 https 的网站显示内容有疑问吗?该代码一直有效,直到服务器上的所有站点都获得 ssl。也许证书是 tlss 1.2?所以我试图从中执行此操作的站点现在拥有此证书。
$data = file_get_contents('https://www.ladygaga.com/');
echo $data;
Based on OpenSSL changes in PHP 5.6,试试这个:
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents("https://www.ladygaga.com/", false, stream_context_create($arrContextOptions));
echo $response;
另一种选择是这样使用 curl:
function file_get_contents_curl( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
$data = curl_exec( $ch );
curl_close( $ch );
return $data;
}
$data = file_get_contents_curl("https://www.ladygaga.com");
根据php.net
When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a close_notify indicator. PHP will report this as "SSL: Fatal Protocol Error" when you reach the end of the data. To work around this, the value of error_reporting should be lowered to a level that does not include warnings. PHP can detect buggy IIS server software when you open the stream using the https:// wrapper and will suppress the warning. When using fsockopen() to create an ssl:// socket, the developer is responsible for detecting and suppressing this warning.