How to Check Courier Service availability and Courier cost by Pin Code using Shiprocket API in PHP


In this example shows, how to check courier availability by pin code and calculate the courier cost using Shiprocket API in PHP.

Shiprocket provides API to check the availability of couriers between the pickup and delivery postal codes. This API returns a list of available courier companies, cost, Estimated delivery days and Courier details.

There are two API available to check Courier Service availability

  • Check Domestic Courier Serviceability
  • Check International Courier Serviceability

The following PHP code to check Domestic and International Courier Serviceability and courier cost details by pincode.

 
<?php 
  /* ------------------- Create Class For API Inegration ------------------- */
  class ShiprocketAPI{
    private $user_name; 
    private $password; 
    
    public function __construct(){
      $this->user_name="username";
      $this->password="password";
    }
    
    #validate your API user
    #use token to validate your API calls.
    function getToken(){
      
      $user=[
        "email"=>$this->user_name,
        "password"=>$this->password,
      ];
      
      #array to JSON
      $login_user=json_encode($user);
      
      $curl = curl_init();
      
        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://apiv2.shiprocket.in/v1/external/auth/login",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS =>$login_user,
          CURLOPT_HTTPHEADER => array(
          "Content-Type: application/json"
          ),
        ));
      
      $response = curl_exec($curl);
      curl_close($curl);
      
        $res=json_decode($response);
      
      if(isset($res->token)){
        $result=[
          "status"=>true,
          "token"=>$res->token,
        ];
      }else{  
        $result=[
          "status"=>false,
          "message"=>$res->message,
        ];
      }
      return $result;
    }
    
    #Check Courier Serviceability and Calculate Couries Charges
    function checkCourierAvailAndRate($details){
      
      $result=$this->getToken();
      
      $data=json_encode($details);
      
      if($result["status"]){
        
        $curl = curl_init();
        curl_setopt_array($curl, array(
        
          CURLOPT_URL => "https://apiv2.shiprocket.in/v1/external/courier/serviceability/",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_POSTFIELDS =>$data,
          CURLOPT_HTTPHEADER => array(
          "Content-Type: application/json",
          "Authorization: Bearer {$result["token"]}"
          ),
        ));
        
        $response = curl_exec($curl);
        curl_close($curl);
        $res=json_decode($response);
        return $res;
        
      }else{
        $result=[
          "status"=>false,
          "message"=>$result["message"],
        ];
        return $result;
      }
    }
    
    #Check International Courier Serviceability and Calculate Couries Charges
    function checkInternationalCourierAvailAndRate($details){
      
      $result=$this->getToken();
      
      $data=json_encode($details);
      
      if($result["status"]){
        
        $curl = curl_init();
        curl_setopt_array($curl, array(
        
          CURLOPT_URL => "https://apiv2.shiprocket.in/v1/external/courier/international/serviceability",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_POSTFIELDS =>$data,
          CURLOPT_HTTPHEADER => array(
          "Content-Type: application/json",
          "Authorization: Bearer {$result["token"]}"
          ),
        ));
        
        $response = curl_exec($curl);
        curl_close($curl);
        $res=json_decode($response);
        return $res;
        
      }else{
        $result=[
          "status"=>false,
          "message"=>$result["message"],
        ];
        return $result;
      }
    }
  }
  
  $obj=new ShiprocketAPI();

  /* ------------------- Domestic Courier Serviceability Details ------------------- */
  $details=[
    "pickup_postcode"=>636005,
    "delivery_postcode"=>630001,
    "cod"=>1,     #1-Cash,0-Prepaid 
    "weight"=>1,    #weight in kgs
    "mode"=>"Surface",  #Either: Surface or Air
  ];
  
  
  $result=$obj->checkCourierAvailAndRate($details);
  // echo "<pre>";
  // print_r($result);
  
  
  /* ------------------- International Courier Serviceability Details ------------------- */
  $details=[
    "pickup_postcode"=>636005,
    "delivery_country"=>"US", #country ISO Alpha 2 code.
    "cod"=>0,       #Cash on delivery status. Must be 0.
    "weight"=>1.5,    #weight in kgs      
  ];
  $result=$obj->checkInternationalCourierAvailAndRate($details);
  // echo "<pre>";
  // print_r($result);
?>