[英]PHP - file_get_contents() works perfectly on localhost but on live server
I have been trying to get the html content from a webpage using file_get_contents().我一直在尝试使用 file_get_contents() 从网页中获取 html 内容。 It works perfectly on localhost but not on live server.
它可以在本地主机上完美运行,但不能在实时服务器上运行。
Some more information is as follows:更多信息如下:
false
false
的$url = 'https://trafficsecrets.com/ts-free-book';
$context = stream_context_create(
array (
'http' => array (
'method' => 'GET',
'follow_location' => true,
'max_redirects' => 5,
'header' => array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201', ),
),
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
)
);
echo var_dump(file_get_contents($url, false, $context));
Why this code gives the html on localhost and not on the server?为什么这段代码在本地主机上而不是在服务器上给出 html?
Maybe try this to catch the error:也许试试这个来捕捉错误:
set_error_handler(
function ($severity, $message, $file, $line) {
throw new ErrorException($message, $severity, $severity, $file, $line);
}
);
try {
file_get_contents($url, false, $context));
}
catch (Exception $e) {
echo $e->getMessage();
}
restore_error_handler();
Source: How can I handle the warning of file_get_contents() function in PHP?资料来源: 如何处理 PHP 中的 file_get_contents() function 的警告?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.