如何解决PHP-file_get_contents可以在localhost上正常运行,但可以在实时服务器上运行
我一直在尝试使用file_get_contents()从网页获取html内容。它可以完美地在localhost上运行,但不能在实时服务器上运行。
更多信息如下:
- PHP版本为7 +
- http,https包含在已注册的PHP流中
- allow_url_fopen已打开
- 使用了cURL,但它重定向到其他页面,并使用我的域作为引荐来源网址。
- 通过将代码放入PHP文件在浏览器中执行
- 以下代码在实时服务器上的输出为
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?
解决方法
也许可以尝试捕获错误:
set_error_handler(
function ($severity,$message,$file,$line) {
throw new ErrorException($message,$severity,$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?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。