从PHP脚本访问页面时有趣的500错误

时间:2012-04-02 07:30:33

标签: php html html-parsing simple-html-dom

我想解析不需要身份验证的http://gmmobile.atm-mi.it/wsbw/InfoTraffico/页面。然而,当我尝试通过PHP脚本访问它以解析其内容时,我收到一个错误:

  

警告:file_get_contents(http://gmmobile.atm-mi.it/wsbw/InfoTraffico/)[function.file-get-contents]:无法打开流:HTTP请求失败!第70行/iPhone/simplehtmldom_1_5/simple_html_dom.php中的HTTP / 1.1 500内部服务器错误

我使用的代码如下:

include('simple_html_dom.php');
// Create DOM from URL or file
$url='http://gmmobile.atm-mi.it/wsbw/InfoTraffico/';
echo $url;
$html = file_get_html($url);
echo $html;
$i=0;
foreach($html->find('option') as $content) 
{
    $linea=$content->value;
    $destination=$content->title;
        $i++;

}

可能是什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试添加用户代理:

$url = 'http://gmmobile.atm-mi.it/wsbw/InfoTraffico/';
$options = array('http' => array('header' => "User-Agent: myFooAgent\r\n"));
$context = stream_context_create($options);
echo file_get_contents($url, false, $context);