CURL in php with custom headers

September 17, 201884
Share Tweet Pin it

Get understand the CURL in PHP. This helps us to send request to any url in php. The main implementation of CURL is to call APIs in php code.

Lets make the data you want to send:

$array=array(
    "name"=>"This is My name",
    "age"=>"30",
    "emailid"=>"emailid@domain.com"
);
$postvars = '';
if (!empty($array)) {
     foreach ($array as $key => $value) {
          $postvars .= $key.'='.$value.'&';
     }
}

First of all we have to initialize the curl for that we have use the following code

$ch = curl_init();

Set the URL to send request to:

$url = 'https://54.85.22.60/server/api/';
curl_setopt($ch, CURLOPT_URL, $url);

Now time to set the method of the request you want to send is GET or POST:

//0 for a get request
curl_setopt($ch, CURLOPT_POST, 1);                           

Lets add the data to be sent to the url:

if ($postvars != '') {
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
}

Lets set the header information and if no response timeout:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
if ($token != '') {
     $headers = array(
         'Authorization: Bearer '.$token,
         'Content-Type: application/json;charset=utf-8',
     );
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

In the above code I am sending you an authorization token which I have stored into $token.

The following step is the final step in curl request. I have used here json_decode to convert the object into respective array which will be easy to access throughout your program.

$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);

In last I have used the return if you are using the entire code inside a function else you can use it directly inside any variable. Please follow the entire code as below.

            $ch = curl_init();
            $postvars = '';
            if (!empty($arraydata)) {
                foreach ($arraydata as $key => $value) {
                    $postvars .= $key.'='.$value.'&';
                }
            }

            $url = 'http://54.15.21.60/server/api/';   //API url
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);         //0 for a get request
            if ($postvars != '') {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
            curl_setopt($ch, CURLOPT_TIMEOUT, 20);
            if ($token != '') {
                $headers = array(
                    'Authorization: Bearer '.$token,
                    'Content-Type: application/json;charset=utf-8',
                );
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
            $response = curl_exec($ch);
            curl_close($ch);
            return json_decode($response, true);

Hope this helps you friends. Thanks for be here.