In this article, I am going to explain how to fetch twitter data with the use of cURL and to get a stream of JSON from Twitter
Twitter very helpfully allows you to fetch information.you just have to send a JSON string to the JSON URL and you will get a JSON string back then you can use json_decode to transform the JSON string to PHP array.
Let’s see the following code:
$url = "http://twitter.com/users/show.json?screen_name=creativedevs"; //screen_name is the name of twitter $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, "$url"); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); $cl_data = curl_exec($curl_handle); curl_close($curl_handle); $data = json_decode($cl_data); // To decode data which is in JSON format print_r($data);
Above example, fetch all data from the twitter. but you can use data you want to.This function is a very basic example of CURL request.
NOTE: you can also fetch data in XML format by using the URL http://twitter.com/users/show.xml?screen_name=username
By the end, I suggest you to try: