用简单的xml阅读twitter的rss搜索Feed

时间:2009-11-24 00:31:01

标签: php xml twitter simplexml

在rss Feed中为twitter的搜索选择一些节点时遇到一些麻烦

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>MarielaCelita@twitter.com (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 feed,如果有20个结果,它循环20次,只是没有数据

4 个答案:

答案 0 :(得分:2)

  1. 在代码中,为$ screenname分配了一个值,但您正在回显$ author。
  2. 要获取google:image_link等命名空间中的元素,您必须执行以下操作:
  3. $g = $key->children("http://base.google.com/ns/1.0"); $profimg = $g->{"image_link"};

    如果您想知道我从哪里获得"http://base.google.com/ns/1.0",则会在rss Feed的第二行中提及名称空间。

    $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 :(得分:1)

设置error_reporting(E_ALL);,您会看到$author未定义。

你不能以这种方式访问​​<google:image_link/>,你必须使用XPath或children()

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

如果您使用SimpleDOM,则会有一个快捷方式返回XPath结果的第一个元素:

$key->firstOf("google:image_link");

答案 2 :(得分: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,,,,,

    }

答案 3 :(得分: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] => Alex_Segal@twitter.com (Alex Segal)
)
 --> 

您可以在foreach外观中使用任何内容并回显它们,例如$ item-&gt; author,$ item-&gt; link等....您可以使用getattribute函数的任何其他属性... < / p>