views:

818

answers:

4

Having some trouble selecting some nodes in the rss feed for twitter's search

the rss url is here

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

each item looks like this

<item>
  <title>RT @TwittBoy: TwitFile - Comparte tus archivos en Twitter (hasta 200Mb) http://bit.ly/xYNsM&lt;/title&gt;
  <link>http://twitter.com/MarielaCelita/statuses/5990165590&lt;/link&gt;
  <description>RT &lt;a href=&quot;http://twitter.com/TwittBoy&amp;quot;&amp;gt;@TwittBoy&amp;lt;/a&amp;gt;: &lt;b&gt;TwitFile&lt;/b&gt; - Comparte tus archivos en Twitter (hasta 200Mb) &lt;a href=&quot;http://bit.ly/xYNsM&amp;quot;&amp;gt;http://bit.ly/xYNsM&amp;lt;/a&amp;gt;&lt;/description&gt;
  <pubDate>Mon, 23 Nov 2009 22:45:39 +0000</pubDate>
  <guid>http://twitter.com/MarielaCelita/statuses/5990165590&lt;/guid&gt;
  <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"/&gt;
  <google:image_link>http://a3.twimg.com/profile_images/537676869/orkut_normal.jpg&lt;/google:image_link&gt;
</item>

My php is below

  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>
";

Problem is nothing is being echoed, i mean from the rss feed, if there are 20 results, its looping 20 times, just no data

+1  A: 

Set error_reporting(E_ALL); and you'll see that $author isn't defined.

You can't access <google:image_link/> this way, you'll have to use XPath or children()

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

If you use SimpleDOM, there's a shortcut that returns the first element of an XPath result:

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

    }
streetparade
+1  A: 
  1. In the code, $screenname is assigned a value but you are echoing $author.
  2. To get elements within namespaces like google:image_link ,you will have to do this:

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

If you are wondering where did I get "http://base.google.com/ns/1.0" from, the namespace is mentioned in the second line of the 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;
}

This code works.

vsr
A: 

I made this and it works :)) $sea_name is the keyword your looking for...

<?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;
        }
    }
}
?>

The object will contain somthing like:

<!-- 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)
)
 -->

You can use any of it inside the foreach look and echo them such as $item->author, $item->link, etc....any other attributes you can use the getattribute function...

Shadi Almosri
and yes the usage of twits (authors) and what they say tends to mostly be "blah" like content, so don't knock the variable names! :)
Shadi Almosri