如何在 Simple HTML Dom Parser 中处理 http 错误
How to handle http errors in Simple HTML Dom Parser
所以我有这个,
$r1pro = file_get_html('http://www.example.com/forums-profile.php?user_id='.$uid);
$uname = $r1pro->find('span.username', 0);
它运行完美,但网站设计不是很好,如果您没有登录,一些请求 return 500 错误,输出如下:
Warning: file_get_contents(http://www.example.com/forums-profile.php?user_id=12345678): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /home/mysite/public_html/su3/htmldom.php on line 76
Fatal error: Call to a member function find() on boolean in /home/mysite/public_html/su3/index.php on line 61
我在 simplehtmldom 中搜索了错误处理方法,但一无所获。有什么方法可以捕获 http 错误并 运行 一些不同的代码吗?
没关系,我现在觉得自己很傻。我所要做的就是
if($r1pro){
<--do normal stuff if no error-->
}else{
<--do stuff in case of error-->
}
所以我有这个,
$r1pro = file_get_html('http://www.example.com/forums-profile.php?user_id='.$uid);
$uname = $r1pro->find('span.username', 0);
它运行完美,但网站设计不是很好,如果您没有登录,一些请求 return 500 错误,输出如下:
Warning: file_get_contents(http://www.example.com/forums-profile.php?user_id=12345678): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /home/mysite/public_html/su3/htmldom.php on line 76
Fatal error: Call to a member function find() on boolean in /home/mysite/public_html/su3/index.php on line 61
我在 simplehtmldom 中搜索了错误处理方法,但一无所获。有什么方法可以捕获 http 错误并 运行 一些不同的代码吗?
没关系,我现在觉得自己很傻。我所要做的就是
if($r1pro){
<--do normal stuff if no error-->
}else{
<--do stuff in case of error-->
}