The*_*ish 5 php post curl http
我正在使用 API,当我向他们发送 POST 请求时,他们会发出警告,指出数组中的一个字段必须是整数类型,而不是字符串。
我的 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 );
Run Code Online (Sandbox Code Playgroud)
当我将它发送到我的本地主机上的另一个 URL 和 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"
}"
Run Code Online (Sandbox Code Playgroud)
这里的问题是 interval_count 是一个字符串而不是一个整数。如果我在使用 CURLOPT_POSTFIELDS 之前 var_dump 它是一个整数,所以 CURL 部分中的某些内容正在改变它,但我不确定是什么。
该 API 适用于名为 chartmogul.com 的网站
小智 1
来自 ChartMogul 的比尔在这里。您需要使用 来编码您的数据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`
Run Code Online (Sandbox Code Playgroud)