Back to Top

How to Fetch Twitter data using cURL

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 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 = "";

//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);

$data = json_decode($cl_data);

// To decode data which is in JSON format

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

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)

  1. a script to read that ‘anti-spam’ captcha can be written in less lines than ur script.

    2 colors, no distortion. cant make it easier.

    1. Yes, you can reduce script by removing those lines also. 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use HTML5 Page Visibility API

Posted on 3 years ago


In depth Understanding of Laravel Core

Posted on 3 months ago


Sliding Thumbnail and Caption with jQuery

Posted on 7 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago