我正在使用一个API,当我向他们发送POST请求时,他们会抛出一个警告,数组中的一个字段必须是Integer类型,而不是String。
我的CURL设置如下:
$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 );
当我把它发送到另一个URL在我的localhost和var_dump它,我得到这个:
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"
}"
这里的问题是interval_count是一个字符串而不是整数。如果我在使用CURLOPT_POSTFIELDS之前使用var_dump,它是一个整数,所以CURL部分的某些东西正在改变它,但我不确定是什么。
该API用于一个名为chartmogul.com的网站
正如这里的文档所说(https://dev.chartmogul.com/docs/import-plan),您必须发送JSON数据。
应该使用CURLOPT_POSTFIELDS => json_encode($post_fields)
CURLOPT_POSTFIELDS => $post_fields
。编辑:另外,文档说您必须发送5个参数,您忘记了一个必需的名为"data_source_uuid"的参数,该字符串包含此订阅计划的数据源的ChartMogul UUID。
来自ChartMogul的Bill。您需要用json_encode($data)
编码您的数据。请确保您的数据源UUID、帐户密钥和帐户令牌正确。下面的请求适合我:
<?php
// account variables
$ds_uuid = "DATA_SOURCE_UUID";
$token = 'API_TOKEN';
$password = 'SECRET_KEY';
// request url
$baseurl='https://api.chartmogul.com/v1/';
$url=$baseurl.'import/plans';
// data to be posted
$post_fields = array(
'data_source_uuid' => "$ds_uuid",
'name' => 'A plan',
'interval_count' => 1,
'interval_unit' => 'month',
'external_id' => 'eur_fees'
);
// encode json data
$data = json_encode($post_fields);
// initialize cURL
$ch = curl_init();
// set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$token:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
// make the request
$result = curl_exec($ch);
// decode the result
$json = json_decode($result, true);
// print the result
print $json;
curl_close($ch);
?>`enter code here`