2012-02-25 11 views
0

ちょうど 'channel'タグの名前、つまりCHANNELを取得したいと思います...スクリプトを使ってGoogleからrssを解析すると......... .....しかし、私は他のいくつかのプロバイダのためにそれを使用すると出力 '#text'を与える​​のではなく、目的の出力である 'channel'を提供しています。PHP cURLを正しく使ってrssを解析していません

$url = 'http://ibnlive.in.com/ibnrss/rss/sports/cricket.xml'; 
    $get = perform_curl($url); 
    $xml = new DOMDocument(); 
    $xml -> loadXML($get['remote_content']); 
    $fetch = $xml -> documentElement; 
    $gettitle = $fetch -> firstChild -> nodeName; 
    echo $gettitle; 
    function perform_curl($rss_feed_provider_url){ 

     $url = $rss_feed_provider_url; 
     $curl_handle = curl_init(); 

     // Do we have a cURL session? 
     if ($curl_handle) { 
      // Set the required CURL options that we need. 
      // Set the URL option. 
      curl_setopt($curl_handle, CURLOPT_URL, $url); 
      // Set the HEADER option. We don't want the HTTP headers in the output. 
      curl_setopt($curl_handle, CURLOPT_HEADER, false); 
      // Set the FOLLOWLOCATION option. We will follow if location header is present. 
      curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); 
      // Instead of using WRITEFUNCTION callbacks, we are going to receive the remote contents as a return value for the curl_exec function. 
      curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); 

      // Try to fetch the remote URL contents. 
      // This function will block until the contents are received. 
      $remote_contents = curl_exec($curl_handle); 

      // Do the cleanup of CURL. 
      curl_close($curl_handle); 

      $remote_contents = utf8_encode($remote_contents); 

      $handle = @simplexml_load_string($remote_contents); 
      $return_result = array(); 
      if(is_object($handle)){ 
       $return_result['handle'] = true; 
       $return_result['remote_content'] = $remote_contents; 
       return $return_result; 
      } 
      else{ 
       $return_result['handle'] = false; 
       $return_result['content_error'] = 'INVALID RSS SOURCE, PLEASE CHECK IF THE SOURCE IS A VALID XML DOCUMENT.'; 
       return $return_result; 
      } 

     } // End of if ($curl_handle) 
     else{ 
     $return_result['curl_error'] = 'CURL INITIALIZATION FAILED.'; 
     return false; 
     } 
    } 

答えて

2

it gives an output '#text' instead of giving 'channel' which is the intended output$fetch -> firstChild -> nodeTypeTEXT_NODEまたは単にいくつかのテキストである、3であるので、それが起こります。あなたは に

echo $fetch->getElementsByTagName('channel')->item(0)->nodeName; 

によって
$gettitle = $fetch -> firstChild -> nodeValue; 
var_dump($gettitle); 

をチャネルを選択することができます

string(5) " 
    " 

またはスペースやフォーマットによるXMLタグの間に表示されるようにたまたま改行記号を与えます。

PS:あなたのリンクによると、RSSフィードはhttp://validator.w3.org/feed/

0

で検証に失敗したXMLを見てください - それは正しく解析されているので、それはかなり空白で印刷されています。ルートノードの最初の子はテキストノードです。より簡単な時間が必要な場合はSimpleXMLを使用するか、興味のあるタグを取得するためにDomDocumentのXPathクエリを使用することをお勧めします。

ここでは、SimpleXMLを

$xml = new SimpleXMLElement($get['remote_content']); 
print $xml->channel[0]->title; 
を使用したい方法です
関連する問題