Issue
I have an API to track the shipment, the API request is below and worked well as excepted,
curl -X POST
--header '17token:xxxxxxxxxxxxxxxxxxxxxxxxx'
--header 'Content-Type:application/json'
--data '[{"number":"RR123456789CN"}]'
https://api.17track.net/track/v1/register
Here is my cURL code to get the response.
<?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;
}
?>
The above code is showing error.
{"code":0,"data":{"errors":[{"code":-18010013,"message":"Submitted data is invalid."}]}}
I have tried all kinds of data structures like :
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\"}]",
but all the above data structures are failing and showing the same error.
I think I am doing something wrong with X POST / POST or
I am failing to request properly.
Edit
After doing some research I have written new code..
<?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);
?>
Now i am getting {"code":401,"data":{"errors":[{"code":-18010002,"message":"Access token is invalid."}]}}
error.
Note : Token is valid and correct, i think something is wrong with my code.
Thank you.
Solution
You have to send your data as raw body:
$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;
Answered By - A.Seddighi