php curl json post being truncated when sent
Get the solution ↓↓↓I am attempting to send a json string in a curl post. My vendor is telling me the request is being truncated. I am at a loss.
my code:
$url = "https://someServer/json.pl/vendor_account_numbers/list";
$key = "XXX";
$secret = "XXX";
$hash = hash_hmac('SHA1',$url,$secret,$raw_output = false);
$array = array(
"json" => array(
"criteria" => array(
"vendor_id" => array(
"operator" => "=","data" => "2"
)
)
)
);
$post = json_encode($array);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'HMAC:'.$hash,
'KEY:'.$key,
'Content-Type: application/json charset=utf-8',
'Content-Length: '.strlen($post),
));
echo strlen($post);
curl_setopt($ch,CURLOPT_POSTFIELDS,"json=".$post);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
print_r(curl_getinfo($ch));
print_r($result);
$res = json_decode($result);
exit();
The vendor says they are seeing:
json:{"criteria":{"vendor_id":{"operator":"=","data":"
Answer
Solution:
I was getting the strlen of the post, but in my curl i was adding "json=", thus making the content-length incorrect.
$post = json_encode($array);
$length = strlen($post)+5;
I did 5 because of the json bit in my postfields:
curl_setopt($ch,CURLOPT_POSTFIELDS,"json=".$post);
Answer
Solution:
Try changing this:
'Content-Type: application/json charset=utf-8',
to this:
'Content-Type: application/json; charset=utf-8',
Note the semi-colon after the mime type.
If the system doesn't have a content-length requirement (many browsers and some web services do), then set that to zero.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 8.0.2".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.