A PHP Function To Send All Kind Of API Curl Requests

Here I’m giving an example of a php function which can send all the curl requests to your desired API service,say facebook,twitter…etc..Hope it will help someone.th
eg:

$params=array(
                               'param1'=>'val1',
                               'param2'=>'val2',
                               'param3'=>'val3'
                              );
    $header=array(
                              'header1:value1',
                              'header2:value2',
                              'header3:value3'
                           );
  sendRequest("example.com/api/v1/test","GET",$params,$headers);
Definition:
 public function sendRequest($url,$type="GET",$params=array(),$headers=array())
    {
         $fields_string = http_build_query($params);
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
       if($type=="GET")
       {
         curl_setopt($ch,CURLOPT_URL, $url."?".$fields_string);
       }
       else if($type=="POST")
       {
         curl_setopt($ch,CURLOPT_POST, 1);
         curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
         curl_setopt($ch,CURLOPT_URL, $url);
       }
       else if($type=="PUT")
       {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch,CURLOPT_URL, $url);
       }
       else if($type=="DELETE")
       {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
         curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
         curl_setopt($ch,CURLOPT_URL, $url);
       }
      $response = curl_exec($ch);
        if($response === false) {
            $error = curl_errno($ch);
            $message = curl_error($ch);
            curl_close($ch);
            throw new CHttpException(404,"Network error " . $message . " (" . $error . ")");
        }
        // Check status code
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if($statusCode != 200) {
            throw new CHttpException($statusCode,$response);
        }
        try {
            $json = json_decode($response);
        } catch (Exception $e) {
             throw new CHttpException($response['statusCode'],"Invalid response body:". $response['body']);
        }
        if($json === null) {
             throw new CHttpException($response[‘statusCode’],"Invalid response body:". $response['body']);
        }
        if(isset($json->error)) {
            throw new CHttpException($response['statusCode'],"Json error:". $json->error);
        } else if(isset($json->errors)) {
            throw new CHttpException($response['statusCode'],"Json error:". implode($json->errors, ', '));
        }
      return(json_decode($response));
    }

Nintriva

Leave a Reply

Your email address will not be published. Required fields are marked *