PHP: Call API using Curl

We are writing this post to describe how to call API through PHP script using CURL method. So it helps you to develop an APP and to call the different third-party API.

This is an example. Firstly, We are retrieving the access token by calling API. Secondly, we are calling another API to get data using first access token with passing post data.

<?php

    /*
    * Get access token
    *
    */

    $baseUrl = 'http://www.example.com/';
    $apiUrl = $baseUrl . 'api/key/validations'; 

    /* REST API Authentication */
    $data = array("client_id" => '1', "client_secret" => 'JKAeMB57',"project_key" => '4aec93');
    $data_string = json_encode($data);  
                                                                                                                                      
    try{
        $ch = curl_init($apiUrl); 
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($data_string))                                                                       
        );       
        $curlData = curl_exec($ch);
        $curlData = json_decode($curlData);
        if(isset($curlData->message)){
            echo $curlData->message;
        }else{
            $access_token = $curlData->access_token;
        }
    
        curl_close($ch);

        echo "<pre>";
        
        //print_r($curlData);
         

       // echo "Access token####: " . $access_token;
       
       

        /*
        * Call another API using above access token
        */
        $apiUrl = $baseUrl . 'api/project/pull'; 
        $ch = curl_init($apiUrl); 

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);   
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Accept:application/json',
            'Authorization:Bearer '.$access_token
             )
        );
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        $result = curl_exec($ch);
        curl_close($ch);

        $arrayData = json_decode($result, true);

        print_r($arrayData);
        echo "</pre>";
    
    }catch(Exception $e){
        echo 'Error: '.$e->getMessage();
    }

?>

This is another example where we call third-party API using API credentials and with its post data once.

<?php 

    /*
    * Store user info using API
    *
    */
   
    $apiUser = 'test'; 
    $apiPass = '******';
    $apiUrl = 'http://www.example.com/INSERT_CUSTOMER_REGISTRATION_ACTION';

    $headerdata = array("username" => $apiUser, "password" => $apiPass);

    $data_string = json_encode($headerdata);      

    $bodyData['registration_date'] = "2018-10-05 16:10";
    $bodyData['customer_name'] = "Sanjeev Jha";
    $bodyData['customer_mobile'] = "9555222117";
    $bodyData['customer_email'] = "tilkor.team@gmail.com";
    $bodyData['customer_city'] = "New Delhi";
    $bodyData['customer_pin'] = "110096";
    $bodyData['customer_gender'] = "Male";

    $newbodyData['objClass'] = $bodyData;

    $bodyData_string = json_encode($newbodyData); 

    try{
      
        echo "<pre>";
        
        $ch = curl_init($apiUrl); 

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData_string);   
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Accept:application/json',
            'Authorization:' . $data_string
             )
        );

        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        $result = curl_exec($ch);
        curl_close($ch);

        $arrayData = json_decode($result, true);
       
        print_r($arrayData);
        echo "</pre>";
    
    }catch(Exception $e){
        echo 'Error: '.$e->getMessage();
    }

?>