cURL is new to me. I'm trying to integrate an api via PHP cURL. The api that I'm trying to access requires that parameters be sent as key-value pairs, not json. Their example cURL request in their docs is:
curl -i -X POST -d 'api_key=my_api_key' -d
'[email protected]' -d "first_name=Joe" -d "last_name=Doe" -d
"cust_id=cus_401"
https://serviceurl.com/api/create
My code is apparently sending empty parameters to their api.
$service_url = 'https://serviceurl.com/api/create';
$curl = curl_init($service_url);
$email = $this->session->userdata('email');
$postArray = array(
'api_key' => 'my_api_key',
'email' => $email,
);
$curl_post_data = $postArray;
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
echo $curl_response;
echo $info;
Any advice would be greatly appreciated.
application/x-www-form-urlencodedormultipart/form-datadepending upon whether you use a simple array as the POST data orhttp_build_queryto create the data stringCURLOPT_POSTFIELDSto read the description. Basically -If value is an array, the Content-Type header will be set to multipart/form-dataso it depends upon whether the api is expecting urlencoded or multipart data