2012-02-23 10 views
0

DomDocumentを使用してRSSフィードを取得しましたが、奇妙な問題が発生しました。 http://rss.slashdot.org/Slashdot/slashdotのようないくつかのRSSフィードを取得すればうまく動作します。しかし、私が解析しようとしているRSSフィードは、私に問題を与えています:http://www.ryanhache.com/feedDOMDocument :: getElementsByTagNameはPHPでチャンネルを取得しません

これは、チャネルタグを見つけてループすることができないようです。私が継承した関数は以下の通りで、RSS_Retrieve($ url)で呼び出されます。私はこれらの機能に欠けているものは何ですか、または私が引っ張っているフィードに何か問題がありますか?

function RSS_Tags($item, $type) 
{ 
    $y = array(); 
    $tnl = $item->getElementsByTagName("title"); 
    $tnl = $tnl->item(0); 
    $title = $tnl->firstChild->textContent; 

    $tnl = $item->getElementsByTagName("link"); 
    $tnl = $tnl->item(0); 
    $link = $tnl->firstChild->textContent; 

    $tnl = $item->getElementsByTagName("pubDate"); 
    $tnl = $tnl->item(0); 
    $date = $tnl->firstChild->textContent; 

    $tnl = $item->getElementsByTagName("description"); 
    $tnl = $tnl->item(0); 
    $description = $tnl->firstChild->textContent; 

    $y["title"] = $title; 
    $y["link"] = $link; 
    $y["date"] = $date; 
    $y["description"] = $description; 
    $y["type"] = $type; 

    return $y; 
} 

function RSS_Channel($channel) 
{ 
    global $RSS_Content; 

    $items = $channel->getElementsByTagName("item"); 

    // Processing channel 

    $y = RSS_Tags($channel, 0);  // get description of channel, type 0 
    array_push($RSS_Content, $y); 

    // Processing articles 

    foreach($items as $item) 
    { 
     $y = RSS_Tags($item, 1); // get description of article, type 1 
     array_push($RSS_Content, $y); 
    } 
} 

function RSS_Retrieve($url) 
{ 
    global $RSS_Content; 

    $doc = new DOMDocument(); 
    $doc->load($url); 

    $channels = $doc->getElementsByTagName("channel"); 

    $RSS_Content = array(); 

    foreach($channels as $channel) 
    { 
     RSS_Channel($channel); 
    } 

} 
+0

「http:// www.ryanhace.com/feed」のURLは404のようですか? –

+0

バールはURLのスペルを間違えています:http://www.ryanhache.com/feed – Jeff

+0

ここにいるようです。あなたの実際の出力と期待される出力は何ですか? –

答えて

1

コードは正常に動作しているようですが、グローバル変数を使用せずにはるかに簡単に作業を完了できます。

function RSS_Tags($node, $map, $type) { 
    $item = array(); 
    foreach ($map as $elem=>$key) { 
     $item[$key] = (string) $node->{$elem}; 
    } 
    $item['type'] = $type; 
    return $item; 
} 

function RSS_Retrieve($url) { 
    $rss = simplexml_load_file($url); 
    $elements = array('title'=>'title', 'link'=>'link', 
     'pubDate'=>'date', 'description'=>'description'); 
    $feed = array(); 
    foreach ($rss->channel as $channel) { 
     $feed[] = RSS_Tags($channel, $elements, 0); 
     foreach ($channel->item as $item) { 
      $feed[] = RSS_Tags($item, $elements, 1); 
     } 
    } 
    return $feed; 
} 

$url = 'http://www.ryanhache.com/feed'; 
$RSS_Content = RSS_Retrieve($url); 
関連する問題