我一直在尝试使用file_get_contents()从网页获取html内容。它可以完美地在localhost上运行,但不能在实时服务器上运行。
更多信息如下:
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));
为什么此代码在本地主机而不是在服务器上提供html?
答案 0 :(得分:0)
也许可以尝试捕获错误:
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();
来源:How can I handle the warning of file_get_contents() function in PHP?