PHP Curl Examples

php-curl

In previous chapter we have explored various aspects of php curl. In this part of the tutorial we will see some basic php curl examples. In this part of the tutorial we are assuming that you have basic understanding of curl. If you want to learn basics of curl you can read our PHP Curl Tutorial.

To use curl we need to follow below steps:

  1. Init curl
  2. Set options. You can get detail options value from PHP website Curl Option Manual.
  3. Execute curl
  4. Get result

Let us see some example using the above steps of the curl.

Basic  PHP Curl Examples

Below are some basic php curl examples.

  1. Retrieving Webpage
  2. Calling Rest API
  3. Sending POST request.
  4. Sending Receiving XML
  5. Uploading File
  6. Passing Custom HTTP header.

 

Retrieving Webpage in PHP Using Curl

The most basic example of the curl is retrieving webpage using curl. Below is the code to fetch google.com homepage:

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.google.co.in");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>

Output of the above code is html of homepage of http://www.google.co.in.

Calling a Rest API

Rest APIs are nothing but a http request. So you can call rest API by curl using same way which you apply for retrieving the webpage. In most of the cases rest API send output in json format, but not all the time. In some cases rest api need some extra header like authentication, response type etc.

Simple REST API Call

Below is the rest api call. In below example we are calling amazon auto complete api and searching for word facebook. Output of the below code is JSON so we are decoding json in PHP array/object.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://completion.amazon.com/search/complete?search-alias=aps&client=amazon-search-ui&mkt=1&q=facebook");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print_r(json_decode($result));//return amazone autocomplete suggestion
?>

In above example we are not doing any different thing than retrieving the webpage.

Rest API Call With Username and Password

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost/rest.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_USERPWD,"vikas:password");
$result = curl_exec($curl);
curl_close($curl);
print($result);
?>

In above code we are making API call with Username and password.

Sending HTTP POST request

In PHP curl library sending POST request is driven by an option name CURLOPT_POSTFIELDS. IF you want to send raw post string just need to pass the string with the option. If you want to send the form field like post submission then need to pass the array. For raw post request we will demonstrate in Sending and receiving XML section. Below is the code to send form post request using CURL
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost/post_form");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('field1'=>'value1','field2'=>'value2','field3'=>'value3'));//Setting post data as xml
$result = curl_exec($curl);
curl_close($curl);
print($result);
?>

In above example we only doing different code is setting post option. Below code is responsible for sending POST
curl_setopt($curl, CURLOPT_POSTFIELDS, array(‘field1’=>’value1′,’field2’=>’value2′,’field3’=>’value3′));//Setting post data as xml

Sending Receiving XML

Some APIs demands XML raw post data as input parameter.  Below is a example of sending XML raw post data:
<?php
$xml = '<node>'
. '<test>'
. '1'
. '</test>'
. '</node>';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost/test_api");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); //setting content type header
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);//Setting raw post data as xml
$result = curl_exec($curl);
curl_close($curl);
print($result);
?>

In above example first we are setting up content type header of the curl request as application/xml using below code:
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
After setting header we are adding XML in raw post header.

Uploading File Using Curl

Any form which accept the file can only use the POST method. Sending file using CURL is very easy. Below is the example of sending file using curl.

<?php
$file = realpath('test.txt');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost/file_processer");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('file'=>'@'.$file));Setting file path
$result = curl_exec($curl);
curl_close($curl);
print($result);
?>

To upload file you need to get the realpath of the file and send as same as post request and append @ before passing file name.

Passing Custom HTTP header Using CURL

Some of web service require some values in custom http header. For example, some web service require authentication token in http header. We can pass custom http header in curl by setting option CURLOPT_HTTPHEADER. Below is example of passing custom header:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://beingacoder.com");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('custom-header-name: custom-header-value','another-custom-header: another value')); //setting custom header
$result = curl_exec($curl);
curl_close($curl);
print($result);
?>