http - How to preserve Integer in PHP CURL request
I'm working with an API and when I send the POST request to them they throw a warning that one of the fields in the array has to be type Integer, not a String.
My CURL setup is like this:
$post_fields = array(
'data_source_uuid' => $uuid,
'name' => 'TestPlan',
'interval_count' => 1,
'interval_unit' => 'month',
'external_id' => 'Eur_fees'
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_USERPWD => $api_key
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_HTTPHEADER => 'Content-Type: application/json'
));
$result = curl_exec($curl);
curl_close( $curl );
When I send this to another URL on my localhost and var_dump it I get this:
string(253) "array(5) {
["data_source_uuid"]=>
string(39) "uuid"
["name"]=>
string(8) "TestPlan"
["interval_count"]=>
string(1) "1"
["interval_unit"]=>
string(5) "month"
["external_id"]=>
string(8) "Eur_fees"
}"
The problem here is interval_count is a String not an Integer. If I var_dump before using CURLOPT_POSTFIELDS it is an Integer so something in the CURL part is changing it but I'm not sure what.
The API is for a website called chartmogul.com
Answer
Solution:
As the documentation says here (https://dev.chartmogul.com/docs/import-plan), you have to send JSON data.
Instead of
CURLOPT_POSTFIELDS => $post_fields
, you should useCURLOPT_POSTFIELDS => json_encode($post_fields)
EDIT : Also, the documentation says that you have to send 5 parameters, you forgot one required named "data_source_uuid", a string that contains the ChartMogul UUID of the data source for this subscription plan.
Answer
Solution:
Bill from ChartMogul here. You would need to encode your data with
json_encode($data)
. Please also ensure that your data source UUID, account secret key and account token are correct. The following request works for me: