Created
January 12, 2012 21:59
-
-
Save rachelbaker/1603407 to your computer and use it in GitHub Desktop.
Display Latest Tweets Function - uses curl to fetch json timeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// replace $username= with correct username | |
function rb_latest_tweet($username = 'rachelbaker', $include_date = true){ | |
$twitter_feed_url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=0&screen_name=$username&count=3"; // will show 3 most recent tweets, can change value if needed // | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $twitter_feed_url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$alltweets = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
/* Use the tweet ID from XML to retrieve an HTML page with the tweet */ | |
try { | |
$outputs = json_decode($alltweets); | |
/* Twitter #failwhale */ | |
} catch (Exception $e) { | |
return '<div id="tweet">@<a target="_blank" href="http://www.twitter.com/' . $username . '" target="_blank">' . $username . '</a>: <em>No tweet retrieved.</em>r</div>'; | |
} | |
if(is_array($outputs)){ | |
foreach($outputs as $output){ | |
$url = $output->entities->urls[0]->url; | |
$text = $output->text; | |
$text = str_replace($url, ' ', $text); | |
$d_url = $output->entities->urls[0]->display_url; | |
$date = $output->created_at; | |
$date = ( date("g:i A", strtotime($date))." ".date("M jS",strtotime($date)) ); | |
echo '<div class="feed-item"> | |
<p class="feed">@<a target="_blank" href="http://www.twitter.com/' . $username . '" target="_blank">' . $username . ':</a> '.$text.'<a target="_blank" href="'.$url.'">'.$url.'</a></p> | |
<p class="meta">'.$date.'</p> | |
</div>'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment