2009-11-24 117 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