•  0
    PHP

    Simple PHP function to request API using Curl

      Admin     2092        0        Report content

    Simple PHP function to request API using Curl. This function will convert the xml data into array.

     

    function executeAPI($path, $method ='POST', $json='')
    {
        $url =$path;
        $curl = curl_init("REMOTE XML FILE");
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($curl);
        if($data === false){
            $error = curl_error($curl);
            error_log('API failed: '.$error); 
        }else{
        	//Convert xml to array
            $data = json_decode(json_encode(simplexml_load_string($data)), true);
        }
        curl_close($curl);
        return $data;
    }
    

     

     


  •  


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations