1

I am developing the web server using other service by CodeIgniter. So I am going to send the data to the service using api calling. But I have a issue to make the request.

This is the data what I want to send.

$datastr = 'proc_name=customer_upd&params={ 
"proc_info":
{ 
    "proc_division":"U"
}, 
"data":[{ 
    "table_name":"Customer", 
    "rows":[ {
                    "itemId" : "12231551",
                    "lastName" : "ads",
                    "firstName" : "fds"
   } ] 
}] 
}'

I made my code for this.

$curl = curl_init('https://webapi.ooo.com/access/');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($curl, CURLOPT_POSTFIELDS, $datastr);
curl_exec($curl);

But this is not work. I am tested by postman, but it works well. I think there is a issue to make the post request. Please help me. Thank you.

LoveCode
  • 69
  • 3
  • 9

1 Answers1

2

I think you are sending query string, instead, send only data in the key-value pair to the web service you are consuming. Check in which form web service is accepting the data. First, check web service from postman

Here is an example,

$data = array("key_1" =>value_1 , "key_2" =>"value_2" , "key_3" =>"value_3" , "key_4" =>"value_4" , "key_5" =>"value_5" );

 function api($url,$api,$data)
 {
                $ch = curl_init();
                $webservicelink = $url.$api;
                curl_setopt($ch, CURLOPT_URL, $webservicelink);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                'Content-Type: application/json'));      
                $result = curl_exec($ch);
                return json_decode($result, true);
 }