Created
June 28, 2011 23:39
-
-
Save ethangardner/1052515 to your computer and use it in GitHub Desktop.
YQL Twitter Dashboard
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
<table id="social-media" class="tablesorter"> | |
<caption>Twitter Dashboard with YQL</caption> | |
<thead> | |
<tr> | |
<th>Account Name</th> | |
<th>Followers</th> | |
<th>Following</th> | |
<th>Last Tweet</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
// assemble the array of twitter accounts | |
$accounts = array('ethangardner', 'yql', 'jquery', 'ydn', 'php_net'); | |
// sort accounts alphabetically | |
sort($accounts); | |
// loop through the array of accounts and assemble the url for the YQL results | |
$accountSize = sizeof($accounts); | |
for($i=0; $i<$accountSize; $i++){ | |
$accounts[$i] = 'select%20*%20from%20twitter.user.profile%20where%20id=\'' . $accounts[$i] . '\''; | |
} | |
// prepare to assemble all urls in combination for the query.multi data in YQL | |
$allAccounts = implode(";", $accounts); | |
$url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20query.multi%20where%20queries%3D%22" . $allAccounts . "%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; | |
// call the url using curl | |
$call = curl_init(); | |
curl_setopt($call, CURLOPT_URL, $url); | |
curl_setopt($call, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($call); | |
curl_close($call); | |
// get the results of curl and assign it to a JSON object | |
$data = json_decode($output); | |
// loop through the results | |
foreach($data->query->results->results as $profile){ | |
$item = $profile->item; | |
$created = $item->meta[10]->content; | |
$lasttweet = $item->item[1]->meta[1]->content; | |
// Make any text in the most recent tweet beginning with 'http://' a clickable link | |
$lasttweet = preg_replace("/(http:\/\/)?([a-zA-Z0-9\-.]+\.[a-zA-Z0-9\-]+([\/]([a-zA-Z0-9_\/\-.?&%=+])*)*)/", '<a href="http://$2">http://$2</a>', $lasttweet); | |
// create table rows for each of the results and assign data to its appropriate cell | |
echo '<tr><td><a href="' . $item->resource . '">' . $item->meta[1]->content . '</td><td>' . $item->meta[5]->content . '</td><td>' . $item->meta[6]->content . '</td><td>' . $lasttweet . '</td></tr>'; | |
} | |
?> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment