PHP is such a straightforward language and with cURL we can fetch any data from certain URL by passing information.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $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:
To create zoho leads using PHP
To read JSON data with jQuery
To get Latitude and longitude using address
Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (2)