如何为X Post设置正确的cURL请求?



我有一个API来跟踪发货,API请求如下,并且正常工作,

curl -X POST 
--header '17token:xxxxxxxxxxxxxxxxxxxxxxxxx'
--header 'Content-Type:application/json'
--data '[{"number":"RR123456789CN"}]'
https://api.17track.net/track/v1/register 

这是我的cURL代码来获得响应。

<?php
// started curl
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.17track.net/track/v1/register",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "number:RR123456789CN", // SOMETHING IS WRONG HERE.
//data '[{"number":"RR123456789CN"}]'
CURLOPT_HTTPHEADER => [
"17token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"header 'Content-Type:application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>

上面的代码显示错误。

{"code":0,"data":{"errors":[{"code":-18010013,"message":"Submitted data is invalid."}]}}

我尝试了各种数据结构,如:


1. CURLOPT_POSTFIELDS => "RR123456789CN", 
2. CURLOPT_POSTFIELDS => "[{number:RR123456789CN}]",

3. CURLOPT_POSTFIELDS => "{number:RR123456789CN}",   

4. CURLOPT_POSTFIELDS => '{number:RR123456789CN}',   

5. CURLOPT_POSTFIELDS => '{number:RR123456789CN}',    

6. CURLOPT_POSTFIELDS => "data '[{number:RR123456789CN}]'",
7. CURLOPT_POSTFIELDS => "{"number": "RR123456789CN"}",
8. CURLOPT_POSTFIELDS => "[{"number": "RR123456789CN"}]",

但是上述所有数据结构都失败并显示相同的错误。

  1. 我想我是做错了X POST/POST或

  2. 我没有正确请求。

编辑

在做了一些研究之后,我写了新的代码。

<?php
// setting the main url
$url = 'https://api.17track.net/track/v1/register';
// the tracking id as array
$post_data = array (
'number' => 'RR123456789CN',);
// start
$ch = curl_init();
// URL
curl_setopt($ch, CURLOPT_URL, $url);
// Returntransfer = true
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Post = true
curl_setopt($ch, CURLOPT_POST, 1);
// Header # 1
curl_setopt($ch, CURLOPT_HEADER, "17token:xxxxxxxxxxxxxxxxxxxxxxxxx");
// Header # 2
curl_setopt($ch, CURLOPT_HEADER, 'Content-Type:application/json');
// posting tracking id
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Getting data in a string ($output)
$output = curl_exec($ch);
// error msg if fails
if ($output === FALSE){
echo "cURL Error:" . curl_error($ch);
}
// turning off the cURL
curl_close($ch);
// Printing the output.
print_r($output);
?>

现在我得到{"code":401,"data":{"errors":[{"code":-18010002,"message":"Access token is invalid."}]}}错误。

注意:令牌是有效和正确的,我认为我的代码有问题。

谢谢。

你必须以raw格式发送数据身体:

$curl = curl_init();
$data = array(0 => array("number" => "CM436202796IN"));
$data = json_encode($data);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.17track.net/track/v1/register',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data, // Mistake is here
CURLOPT_HTTPHEADER => array(
'17token: xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

replace

CURLOPT_CUSTOMREQUEST => "POST",

CURLOPT_POST=>1

和替换

CURLOPT_POSTFIELDS => "number:RR123456789CN", // SOMETHING IS WRONG HERE.

CURLOPT_POSTFIELDS => json_encode(array(
0 => array(
"number" => "CM436202796IN"
)
)),

和替换


CURLOPT_HTTPHEADER => [
"17token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"header 'Content-Type:application/json"
],

CURLOPT_HTTPHEADER => [
"17token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type: application/json"
],

相关内容

  • 没有找到相关文章

最新更新