go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

PHP and The Twitter API

Tuesday, 6th December, 2011  /  PHP Tutorials  /  3 Comments
Twitter is done completely through HTTP, so it should be programming language independent. If you have connection to the internet, you can use the Twitter API. Unless, you want to use the REST half of the API, which you would have to authenticate.

Twitter has 2 APIs for general usage. The first is REST, and the second is Search. REST requires authentication because it has to do with sending data to the server. The Search API is solely for, well, searching through the database!

In this example, I use cURL to access the JSON encoded current trends. I use CURLOPT_RETURNTRANSFER set to True, so it is returned as a string. Then, I decode the JSON and go through what I'm given. The information comes with a timestamp, which I pass by calling array_values twice. From there, the one thing I access is the name of each and every popular goings-on.

The Search API is not heavily rated--which means you can make a lot of requests, but they don't say exactly how it is rated. Therefore, you should not call this every second for a week, but you don't have to check the trends only once a year, either.

Use the Twitter API reference at your will:

<?php
$current = "http://search.twitter.com/trends/current.json";

$c = curl_init();

curl_setopt($c, CURLOPT_URL, $current);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$ce = curl_exec($c);

$trends = json_decode($ce,1);
$trends = array_values(array_values($trends["trends"]));

//print_r($trends[0]);
foreach ($trends[0] as $trend) {
echo $trend["name"] . " ";
}

curl_close($c);
?>
Share on:

3 Comments to "PHP and The Twitter API"

kamal

kamal / June 6, 2014 at 11:31 am

Hello,

Can you please send me a dummy code for trandy tweets. Actually, I didn't get how to configure the code with API.

Thanks
kamal

Hiro

Hiro / November 29, 2012 at 08:53 am

Oh Great Tutorial in that site
thanks for Providing this types of tutorial for us !
very much useful for Learning purpose.

retheesh

retheesh / April 8, 2012 at 21:35 pm

Thanks

Add your comment

Captcha

    Please, be polite and helpful and do not spam or offend others! We promise you will be treated the same way!

    Log in to your account to post your comments. If you still haven't joined our community yet, you can create your FREE account now!

    Posting tip:
    If you use code in your comments, please put it in these tags [php], [sql], [css], [js] PHP code example: [php] echo date("Y-m-d"); [/php]

    Thank you,
    PHPJabbers Team

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.


    Free scripts

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!


    Commercial PHP scripts