What is cURL in PHP?

This article is the first part of the series using curl in php, if you know the basic you can visit Sending post request using curl.


It is a library that helps to receive or transfer data using protocols like DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP.



It is a library used to make HTTP request in PHP.In layman terms, cURL gives the same effect as a user opening a web page in the browser after disabling javascript.When a HTTP request is made in php using curl then the received content contains the source code of the page(after disabling javascript).cURL is a resource, a resource in php is a reference to a data hold by some other module.

Installation of cURL in php can be seen at

Let’s begin with an example to understand curl functionality practically.

Variable $output contains the source code of the web page Now let’s take out title of the page using regular expression.

Above code outputs string(45) “Tushar Sharma | Technophile and PHP developer”.Here we take output as a string and pass in the preg_match and get title of the page.

To get information about any curl request we use a function curl_getinfo(curl resource). This function gives output as follows which contains information about content/type, http code, header size, port, content length and several other parameters related to our request.

Some important cURL options are as follows:

– maximum time required to make a connection to a server
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT , 10 );

– maximum amount of time in seconds to which the execution of individual cURL extension function calls will be limited
curl_setopt($ch,CURLOPT_TIMEOUT , 55 );

– follow url after redirection
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

– verify the certificate’s name against host
curl_setopt($ch , CURLOPT_SSL_VERIFYHOST , true);

– verify the peer’s SSL certificate
curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , true);

– set useragent of the browser to check how webpage looks in that particular browser
curl_setopt($ch, CURLOPT_USERAGENT , Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0 );

– file name to store cookies to
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_location);

– file name to read cookies from
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_location);

Error Handling

Mostly curl_error(curl resource) and curl_errorno(curl resource)are used to get error information during a curl session.curl_error  returns an error in the form of string and curl_errorno return error code.Also you can look for http code if you got any error.In case the request fails, http code could be 404, 503 or anything else but not 200.Check complete list of http code at Wikipedia.

cURL is a very useful library in php which can perform some cool stuff if used wisely.Hope you enjoy the tutorial and tell me what you have done using curl in php.

For detailed reading you can visit curl haxx and

15 thoughts on “What is cURL in PHP?”

  1. Hello There. I found your blog the usage of msn. That is a really neatly written article.
    I’ll make sure to bookmark it and return to learn more of
    your helpful info. Thanks for the post. I will certainly

  2. Howdy! This article could not be written much better!

    Looking at this article reminds me of my previous roommate!
    He continually kept talking about this. I most certainly will send this article
    to him. Fairly certain he’s going to have a great read.
    I appreciate you for sharing!

  3. I’m not that much of a internet reader to be honest but your blogs really nice, keep
    it up! I’ll go ahead and bookmark your website to come back later
    on. All the best

  4. I used to be recommended this web site by way of my cousin. I am not certain whether this submit is written via him as nobody
    else recognize such targeted approximately my problem.

    You’re amazing! Thanks!

  5. I read this article completely about the difference of newest
    and preceding technologies, it’s amazing article.

  6. This is my first time pay a visit at here and i am really
    happy to read all at one place.

  7. Hmm it looks like your website ate my first comment (it was super long) so I
    guess I’ll just sum it up what I wrote and say, I’m
    thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any points for first-time blog writers?
    I’d really appreciate it.

  8. Thanks designed for sharing such a fastidious thinking, post is
    pleasant, thats why i have read it entirely

  9. You actually make it seem so easy with your presentation but I
    find this topic to be really something which I think I would
    never understand. It seems too complicated and extremely broad for me.

    I am looking forward for your next post, I will try to get the
    hang of it!

  10. magnificent post, very informative. I’m wondering why the opposite specialists of this sector do not understand this.
    You should proceed your writing. I am confident, you have
    a huge readers’ base already!

  11. I’ve read several just right stuff here. Definitely price
    bookmarking for revisiting. I surprise how much effort you put to create such a
    wonderful informative website.

  12. What a information of un-ambiguity and preserveness of precious know-how concerning unpredicted feelings.

  13. I like the helpful information you provide on your
    articles. I’ll bookmark your blog and take a look at once more right here regularly.

    I’m reasonably certain I’ll learn lots of
    new stuff right right here! Good luck for the next!

Comments are closed.