How to get image url from tweets using search.twitter?


#1

Hey, I’ve got a twitter website which searches for results which use the phrase ‘FloorPic’. One of the things I want to do is that if the tweet has an image, I want to display it on my website, not have a link to it. Using search.twitter API is there anyway to get the source URL of a tweeted image with the format ATOM

Here is my code if it is of relevance:

 <?php
// get the keyword from url
$srchterm = $_GET["q"];
 
// encode it for the safe search
$srchterm = urlencode($srchterm);
 
// search the keyword using Twitter API https://api.twitter.com/1.1/search/tweets.json
//$srch_twitts = "https://api.twitter.com/1.1/search/tweets.json?q=floorpic";
$srch_twitts = "http://search.twitter.com/search.atom?q=".$srchterm."&rpp=100&include_entities=true";
 
// use curl to execute the Twitter URL
$twits = curl_init();
curl_setopt($twits, CURLOPT_URL, $srch_twitts);
curl_setopt($twits, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($twits);
 
// here we have the searched result in an array
$search_res = new SimpleXMLElement($twi);
 
print_r($search_res);
print_r($result);
?>
<?php
 
/* display the data */
 
$i = 0;
 
// displays the search keyword
echo "<p style='padding-left:10px; color:brown'>Your search term is: " . $srchterm . "</p>";
 
// tweets in an array. split it by foreach
// we need only 10 result so, use if condition
foreach ($search_res->entry as $result) if ($i++ < 100)
{
if (strpos($result->content,'http://t.co/') !== false) {
echo "<div id='tweets' class='image'>";
}
else {
echo "<div id='tweets' class='noimage'>";
}
echo "<div class='avatar'><img src='". $result->link[1]->attributes()->href. "' /> </div>";
echo "<div class='twitcont'>";
echo "<div class='name'>". $result->author->name . "</div>";
echo "<div class='content'>" . $result->content ;
if (strpos($result->content,'http://t.co/') !== false) {
    $position = mb_strpos($result->content,'http://t.co/');
    $url = substr($result->content, -26, -4);
    echo "Expanded URL:".$result->entities->urls->expanded_url;
    echo 'This contains a picture at position '.$position;
}
?>

As you can see towards the end, I have managed to be able to find what is a link so I know what tweets have links and which ones don’t.


#2

Also, To see what I am talking about here is my website:- http://asithappens.host22.com/Twitter/twitteroauth-master/json.html?q=floorpic

The light colored tweets have an image, the dark ones do not.


#3

This question is probably more complicated than you think it is – you’re using a deprecated version of the API (the search.twitter.com API and the ATOM format are both permanently retiring in June 2013). Take a look at the modern Search API which has a a better, more detailed response format: [node:10287]


#4