Ulyssesonline

the tech surfer

  • Home
  • About
  • Archives
You are here: Home / General / Retrieve The Latest Twitter Trends

November 28, 2009

Retrieve The Latest Twitter Trends

Here’s a simple and short tutorial on how to retrieve Twitter trends. We will be using PHP and Twitter’s API to retrieve the latest trends. You can publish the trends on your website, blog or any application you are working on. So, let’s get started.

Twitter has made trends available to anyone via API calls. The url of the API is: http://search.twitter.com/trends.json. Trends is also available in XML, but we will use JSON in this example.

To retrieve the string of data, we will use a PHP command called file_get_contents. We will suppress errors by placing an @ sign in front of file_get_contents. We will assign the string to a variable called $contents. So this is what we have so far.

$contents = @file_get_contents("http://search.twitter.com/trends.json");

$contents = @file_get_contents("http://search.twitter.com/trends.json");

Next, we will check if we have the correct response from the http_header. We will check for the code “200”, meaning we have a valid response. We will use the PHP command called strpos to find the needle in the haystack. The needle in this case is “200.” We will look for it starting at position “0.” The format is going to be like this.

$contents = @file_get_contents("http://search.twitter.com/trends.json");
if (strpos($http_response_header[0], "200")) {
  echo "ok";
} else {
  echo "fail"
}

$contents = @file_get_contents("http://search.twitter.com/trends.json"); if (strpos($http_response_header[0], "200")) { echo "ok"; } else { echo "fail" }

Ok, so we now have a JSON string assigned to the variable named $contents. We just need to decode it using the PHP command called “json_decode” and assign it to the $json array variable. Next, we will run a foreach contruct to echo each trend name.

$contents = @file_get_contents("http://search.twitter.com/trends.json");
if (strpos($http_response_header[0], "200")) {
  $json = json_decode($contents);
  foreach ($json->trends as $trend) {
        echo $trend->name;
      }
} else {
}

$contents = @file_get_contents("http://search.twitter.com/trends.json"); if (strpos($http_response_header[0], "200")) { $json = json_decode($contents); foreach ($json->trends as $trend) { echo $trend->name; } } else { }

So, there you have it. We now have the latest Twitter trends using PHP and Twitter’s API.

Filed Under: General, HTML, PHP Tagged With: api, file_get_contents, json, json_decode, PHP, strpos, twitter

Comments

  1. maha says

    February 28, 2011 at 4:16 am

    i tried this code but there is no result
    help please

  2. Ulysses says

    March 2, 2011 at 6:11 pm

    Not sure what you are doing wrong. What was the error you were getting?

  3. maha says

    March 4, 2011 at 10:02 am

    the result shown on the browser is the print function
    $contents = @file_get_contents(“http://search.twitter.com/trends.js $contents = @file_get_contents(“http://search.twitter.com/trends.json”); if (strpos($http_response_header[0], “200”)) { echo “ok”; } else { echo “fail” } $contents = @file_get_contents(“http://search.twitter.com/trends.json”); if (strpos($http_response_header[0], “200”)) { $json = json_decode($contents); foreach ($json->trends as $trend) { echo $trend->name; } } else { }
    this is displayed on the browser

  4. Ulysses says

    March 4, 2011 at 2:09 pm

    Hi Maha,

    This tells me that you are not using PHP tags. Your code is displayed on the screen instead of being processed by PHP processor on the server. Make sure that you begin your code with PHP tag and end it with ?>. By the way, there is no space between the < and the ? sign.

  5. maha says

    March 4, 2011 at 3:27 pm

    hello,
    here is the complete code that i tried:

    Untitled Document

    trends as $trend) {
    echo $trend->name;
    }
    } else {
    }
    ?>

    there is an error on line 11
    the result that displayed on the browser is:
    trends as $trend) { echo $trend->name; } } else { } ?>

Copyright © 2003 - 2018