2012-02-23 2 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

바의 철자가 잘못되었습니다. 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); 
관련 문제