Curl is used to send and receive data using various protocols. PHP supports libcurl, a library created by Daniel Stenberg. Below is the curl official description.
curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.
Curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more
In this tutorial I am going to show you basic use of curl in PHP. I will also cover curl GET and POST request in this tutorial.
Before we start, first check that curl is enable or not. If you are not sure then check your phpinfo()
How to enable CURL:
By default curl is not enabled in apache and its web sever solutions. You need to enable it in php.ini file. Php.ini file most of the time located in php folder (xampp/mamp/wamp/xampp-vm). Open php.ini and find ;extension=php_curl.dll
. Then remove ";"
before extension=php_curl.dll
. Restart your apache service that’s it.
CURL Basics:
curl_init()
function is used to initialize curl request. curl_exec()
function is used to execute curl. curl_close()
function is used to finish curl session.
Example 1:
1 2 3 4 5 |
$cURL = curl_init('http://example.com/'); $response = curl_exec($cURL); curl_close($cURL); |
In the above example, I used curl_init()
function and pass example.com as a parameter. Then I execute curl using curl_exec function and after that I closed curl_close. When I will hit above code by browser will fetch example.com html and display on my browser screen.
There is another way to send above request.
Example 2:
1 2 3 4 5 6 |
$cURL = curl_init(); curl_setopt($cURL, CURLOPT_URL,'http://example.com/'); $response = curl_exec($cURL); curl_close($cURL); |
You notice that I didn’t pass any domain url in curl_init()
function. I used curl_setopt()
function instead and passed $cURL
, CURLOPT_URL
and http://example.com.
curl_setopt()
function is used to set an option for curl transfer. It takes 3 parameters, first curl handle return by curl_init(), second is CURLOPT_XXX option to set and third is the value to set for CURLOPT_XXX option. CURLOPT_XXX could be any option provided by curl.
Below are some of the common CURLOPT
Option | Value |
---|---|
CURLOPT_HEADER | TRUE to include the header in the output. |
CURLOPT_PUT | TRUE to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE. |
CURLOPT_RETURNTRANSFER | TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. |
CURLOPT_SSL_VERIFYPEER | FALSE to stop cURL from verifying the peer’s certificate. |
CURLOPT_CONNECTTIMEOUT | The number of seconds to wait while trying to connect. Use 0 to wait indefinitely |
CURLOPT_POST | TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms. |
CURLOPT_POSTFIELDS | The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ‘;type=mimetype’. This parameter can either be passed as a urlencoded string like ‘para1=val1¶2=val2&…’ or as an array with the field name as key and field data as value. |
CURLOPT_PROXY | The HTTP proxy to tunnel requests through. |
CURLOPT_USERAGENT | The contents of the “User-Agent: ” header to be used in a HTTP request. |
CURLOPT_USERNAME | The user name to use in authentication |
CURLOPT_USERPWD | A username and password formatted as “[username]:[password]” to use for the connection |
Using curl_setopt options:
Now we know about curl_setopt()
function. Let’s use this function in our example.
Example 3:
1 2 3 4 5 6 7 8 9 10 |
$cURL = curl_init(); curl_setopt($cURL, CURLOPT_URL,'http://example.com/'); curl_setopt($cURL, CURLOPT_HEADER,true); curl_setopt($cURL, CURLOPT_RETURNTRANSFER,true); curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT,10); $response = curl_exec($cURL); curl_close($cURL); echo $response; |
Above code has 4 curl_setopt()
function with different options. CURLOPT_HEADER
is set to true which prints header in the output. CURLOPT_RETURNTRANSFER
is set to true which holds the output in $response
. CURLOPT_CONNECTTIMEOUT
has timeout value 10 seconds. Above code will return below output.
PHP Curl Post Request:
Below is the Curl post request code in which I will post array of user information to example.com. You can use your live url to check this post request. http_build_query()
function generates url-encoded query string for more details visit my post 10 PHP Url Functions – Every beginner must check.
To post data to any website I use and set CURLOPT_POST
option to true. Then I use CURLOPT_POSTFIELDS
option and password encoded data array to third parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$dataArray = array( "name"=>"ahsan", "gender"=>"male", "email"=>"ahsan@example.com", "phone"=>"0000 0000 00", ); $data = http_build_query($dataArray); $url = 'http://example.com/'; $cURL = curl_init(); curl_setopt($cURL, CURLOPT_URL,$url); curl_setopt($cURL, CURLOPT_HEADER,true); curl_setopt($cURL, CURLOPT_RETURNTRANSFER,true); curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT,10); curl_setopt($cURL, CURLOPT_POST, true); curl_setopt($cURL, CURLOPT_POSTFIELDS, $data); $response = curl_exec($cURL); curl_close($cURL); echo $response; |
Also read:
- Simple role based access control example using PHP and MYSQLi
- PHP get random User Agent Function
- PHP read text file and insert into MySQL database
- Create CSV file using PHP and save into directory
- Ajax dropdown list from Database using PHP and jQuery