为什么DOMDocument()在几个URL中有效,而在其他URL中无效


Why DOMDocument() works in several URLs and in others not?

im使用这个简单的脚本解析RSS数据:

<?php
//PUBLIC VARS
$arrFeeds = array();
$downItems = 0;
//*PUBLIC VARS
function getRSS($source) {
    global $arrFeeds, $downItems;
    $doc = new DOMDocument();
    $doc->load($source);
    foreach ($doc->getElementsByTagName('item') as $node) {
        $itemRSS = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue
        );
        array_push($arrFeeds, $itemRSS);
        $downItems+=1;
        //echo($arrFeeds[$TT]['title']."<br>");
    }
}
getRSS("http://www.atm-mi.it/_layouts/atm/apps/PublishingRSS.aspx?web=388a6572-890f-4e0f-a3c7-a3dd463f7252&c=News%20Infomobilita");
echo(strip_tags($arrFeeds[1]['title'])."<br><br>".$arrFeeds[1]['desc']);
?>

这个脚本几乎适用于您尝试的任何rss,但适用于这个特定的rss:http://www.atm-mi.it/_layouts/atm/apps/PublishingRSS.aspx?web=388a6572-890f-4e0f-a3c7-a3dd463f7252&c=News%20Infomobilita它不起作用,rss是有效的,所以我认为这可能是一个安全问题,无论如何,我该如何解决这个问题,让我的脚本更真实?谢谢

我可以通过使用ini_set更改"default_socket_timeout"的值来实现这一点。问题似乎是服务器试图保持连接有效。

$url = 'http://www.atm-mi.it/_layouts/atm/apps/PublishingRSS.aspx?web=388a6572-890f-4e0f-a3c7-a3dd463f7252&c=News%20Infomobilita';
ini_set('default_socket_timeout', 1);
$dom_document = new DOMDocument;
$dom_document->load($url);

或者,您可以考虑使用更灵活的cURL。

$url = 'http://www.atm-mi.it/_layouts/atm/apps/PublishingRSS.aspx?web=388a6572-890f-4e0f-a3c7-a3dd463f7252&c=News%20Infomobilita';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$dom_document = new DOMDocument;
$dom_document->loadXml(curl_exec($ch));
curl_close($ch);