繁体   English   中英

使用简单的xml阅读twitter的rss搜索提要

[英]reading twitter's rss search feed with simple xml

在选择RSS提要中的某些节点进行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次,只是没有数据

  1. 在代码中,为$ screenname分配了一个值,但是您正在回显$ author。
  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 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;
}

此代码有效。

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

您无法通过这种方式访问<google:image_link/> ,必须使用XPath或children()

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

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

$key->firstOf("google:image_link");
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,,,,,

    }

我做到了,它起作用:))$ 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-> author,$ item-> link等。...可以使用getattribute函数的任何其他属性...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM