The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.
The Flash and PHP Bible has a forum for quick support.
100th Tutorial!! Get the retweet count of a URL on Twitter using PHP.
Before starting this tutorial i'd like to mention this is the 100th exclusive tutorial (chosen by the fans) posted on Scriptplayground. That may not seem like a lot in the grand scheme of things, but its quite an accomplishment for a completely free resource. Since I started this site just over 5 years ago I have enjoyed helping fellow programmers and just wanted to say thanks! As you might have noticed big things are happening around here and soon we will be launching version 4 of Scriptplayground.
Okay, now on to the tutorial. I am sure you are familiar with the popularity of Twitter and probably use it yourself. As you know you can retweet a tweet for others to see, but have you ever wanted to see how many times your own link has been retweeted?
Well lets look at a simple script to accomplish just that. Start by creating a new PHP file and define the function that will be called whenever you want to run this code.
function getTweetCount($url)
{
...
}
Now, inside this newly created function define the Twitter endpoint and file loader that calls the Twitter url.
$twitterEndpoint = "http://urls.api.twitter.com/1/urls/count.json?url=%s"; $fileData = file_get_contents(sprintf($twitterEndpoint, $url));
The data that is returned from Twitter is in JSON format, as shown here:
{"count":1280,"url":"http://www.adobe.com/"}
In order to use this in PHP you need to convert it into an array. Luckily PHP has a JSON library for just this task, so lets use that.
$json = json_decode($fileData, true);
The last step in this function is to return the count.
return $json['count'];
As you can see this script is pretty straightforward. Lets complete this tutorial with a quick example.
$url = "http://www.adobe.com"; $retweetCount = getTweetCount($url); print "Count: " . $retweetCount;
There you have it, a complete retweet count in PHP with a working example. Feel free to use/edit this code for you own projects and as always, happy coding!
Here is the complete script, enjoy.
<?php
/*
Scriptplayground
http://v2.scriptplayground.com
THIS SOFTWARE IS PROVIDED BY SCRIPTPLAYGROUND "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL SCRIPTPLAYGROUND OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function getTweetCount($url)
{
$twitterEndpoint = "http://urls.api.twitter.com/1/urls/count.json?url=%s";
$fileData = file_get_contents(sprintf($twitterEndpoint, $url));
$json = json_decode($fileData, true);
unset($fileData); // free memory
return $json['count'];
}
$url = "http://www.adobe.com";
$retweetCount = getTweetCount($url);
print "Count: " . $retweetCount;
?>
Follow Scriptplayground on Twitter (@scriptplay)
©2004 - 2012 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN