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 php.net

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

Variable $output contains the source code of the web page http://tusharsharma.in. 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 php.net.

Sending post request using curl in php

Today we are going to learn about sending post request using curl in php.You can learn about basics of curl in my last article about using curl library in php “what-is-curl-in-php“.

Process of sending post request using curl in php

Find a form to submit using post method.

Get the url where the form get submitted.


Get form fields name.




In above images, i used firebug to check the post request.In first image, i filled the form and opened firebug and click on persist button so that i can see post request. In second image, i have looked for form fields and also you can check the post data string in the image. By using following code, I will send a message by submitting a form i.e. sending data in post using curl.

When i check my mail, hurrah, i got the message.


That’s all for today, hope you enjoy and do try at home.