2009-11-24 77 views
0

有一些麻煩,選擇RSS feed中的一些節點爲Twitter的搜索RSS搜索飼料閱讀Twitter的簡單的XML

的RSS網址是這裏

http://search.twitter.com/search.rss?q=twitfile 

每個項目看起來像這樣

<item> 
    <title>RT @TwittBoy: TwitFile - Comparte tus archivos en Twitter (hasta 200Mb) http://bit.ly/xYNsM</title> 
    <link>http://twitter.com/MarielaCelita/statuses/5990165590</link> 
    <description>RT &lt;a href=&quot;http://twitter.com/TwittBoy&quot;&gt;@TwittBoy&lt;/a&gt;: &lt;b&gt;TwitFile&lt;/b&gt; - Comparte tus archivos en Twitter (hasta 200Mb) &lt;a href=&quot;http://bit.ly/xYNsM&quot;&gt;http://bit.ly/xYNsM&lt;/a&gt;</description> 
    <pubDate>Mon, 23 Nov 2009 22:45:39 +0000</pubDate> 
    <guid>http://twitter.com/MarielaCelita/statuses/5990165590</guid> 
    <author>[email protected] (M.Celita Lijer&#243;n)</author> 
    <media:content type="image/jpg" width="48" height="48" url="http://a3.twimg.com/profile_images/537676869/orkut_normal.jpg"/> 
    <google:image_link>http://a3.twimg.com/profile_images/537676869/orkut_normal.jpg</google:image_link> 
</item> 

我的php低於

foreach ($twitter_xml->channel->item as $key) { 
$screenname = $key->{"author"}; 
$date = $key->{"pubDate"}; 
$profimg = $key->{"google:image_link"}; 
$link = $key->{"link"}; 
$title = $key->{"title"}; 
echo" 
         <li> 
         <a href=$link><img width=48 height=48 src=\"$profimg\"></a> 
         <h5><a href=$link>$author</a></h5> 
         <p class=info><a href=$link>$title</a></p> 
         </li> 
"; 

問題是什麼都沒有迴應,我從RSS提要的意思是,如果有20個結果,它的循環20次,只是沒有數據

回答

2
  1. 在代碼中,$屏幕名被分配一個值,但你是呼應$作者。
  2. 爲了得到這樣google:image_link命名空間內的元素,你將不得不這樣做:

$g = $key->children("http://base.google.com/ns/1.0"); $profimg = $g->{"image_link"};

如果你想知道在那裏我纔拿到"http://base.google.com/ns/1.0"從,命名空間在第二行中提到的RSS飼料。

$url="http://search.twitter.com/search.rss?q=twitfile"; 
$twitter_xml = simplexml_load_file($url); 

foreach ($twitter_xml->channel->item as $key) { 
    $author = $key->{"author"}; 
    $date = $key->{"pubDate"}; 
    $link = $key->{"link"}; 
    $title = $key->{"title"}; 
    $g = $key->children("http://base.google.com/ns/1.0"); 
    $profimg = $g->{"image_link"}; 
    echo" 
          <li> 
          <a href=$link><img width=48 height=48 src=\"$profimg\"></a> 
          <h5><a href=$link>$author</a></h5> 
          <p class=info><a href=$link>$title</a></p> 
          </li> 
    "; 
    $xml = $twitter_xml; 
} 

此代碼有效。

1

設置error_reporting(E_ALL);,你會看到$author沒有定義。

您不能訪問<google:image_link/>這種方式,你必須使用XPath或子女()

$key->children("google", true)->image_link; 

如果使用SimpleDOM,有一個返回的XPath結果的第一個元素的快捷方式:

$key->firstOf("google:image_link"); 
0
if (!$xml = simplexml_load_file('http://search.twitter.com/search.atom?q='.urlencode  ($terms))) 
    { 
     throw new RuntimeException('Unable to load or parse search results feed'); 
    } 
    if (!count($entries = $xml->entry)) 
    { 
     throw new RuntimeException('No entry found'); 
    } 
    for($i=0;$i<count($entries);$i++) 
    { 
     $title[$i] = $entries[$i]->title; 
     //etc.. continue description,,,,, 

    } 
0

我做了這一點,它的工作原理:))$ sea_name是你找......

<?php 
function twitter_feed($sea_name){ 
    $endpoint = 'http://search.twitter.com/search.rss?q='.urlencode($sea_name); // URL to call 
    $resp = simplexml_load_file($endpoint); 

    // Check to see if the response was loaded, else print an error 
    if ($resp) { 
     $results = ''; 
     $counter=0; 
     // If the response was loaded, parse it and build links 
     foreach($resp->channel->item as $item) { 
      //var_dump($item); 
      preg_match("/\((.*?)\)/", $item->author, $blah); 
      $content = $item->children("http://search.yahoo.com/mrss/"); 
         $imageUrl = getXmlAttribute($content, "url"); 
      echo ' 
      <div class="twitter-item"> 
       <img src="'.$imageUrl.'" /> 
       <span class="twit">'.$blah[1].'</span><br /> 
       <span class="twit-content">'.$item->title.'</span> 
       <br style="clear:both; line-height:0;margin:0;padding:0;"> 
      </div>'; 
      $counter++; 
     } 
    } 
    // If there was no response, print an error 
    else { 
     $results = "Oops! Must not have gotten the response!"; 
    } 
    echo $results; 
} 

function getXmlAttribute(SimpleXMLElement $xmlElement, $attribute) { 
    foreach($xmlElement->attributes() as $name => $value) { 
     if($name == $attribute) { 
     return (string)$value; 
     } 
    } 
} 
?> 

對象的關鍵字將包含財產以後這樣的:

<!-- SimpleXMLElement Object 
(
    [title] => Before I go to bed, I just want to say I've just seen Peter Kay's CIN cartoon video for the 1st time... one word... WOW. 
    [link] => http://twitter.com/Alex_Segal/statuses/5993710015 
    [description] => Before I go to bed, I just want to say I&apos;ve just seen <b>Peter</b> <b>Kay</b>&apos;s CIN cartoon video for the 1st time... one word... WOW. 
    [pubDate] => Tue, 24 Nov 2009 01:00:00 +0000 
    [guid] => http://twitter.com/Alex_Segal/statuses/5993710015 
    [author] => [email protected] (Alex Segal) 
) 
--> 

您可以使用任何的它在foreach外觀和回聲他們,如$ item-> author,$ item-> link等....任何其他屬性,你可以使用getattribute函數...

+0

和是的使用twits(作者),他們所說的話往往是「不」的e內容,所以不要敲變量名! :) – 2009-11-24 01:25:56