我正在与外部Restful API进行交互。我能够成功执行GET和POST请求,但我的PUT调用行为异常。
当我尝试使用Postman执行任务时,一切正常。
这些是我的邮递员参数:
URl:https://externalapi.com.br/1/sales/0000-8021-46db-9919-8728fec13556/void?amount=15700
标题:MerchantKey:mykey 标头:MerchantId:myid
我的“卷曲代码”是这样的:
$ch = curl_init('https://externalapi.com.br/1/sales/0000-8021-46db-9919-8728fec13556/void?amount=15700');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'MerchantId: ' . $this->merchant_id, 'MerchantKey: ' . $this->merchant_key ));
$res = null;
try{
$res = curl_exec($ch);
}catch (Exception $e){
return $e->getMessage();
}
return $res;
我的curl代码输出了
Length Required
HTTP Error 411. The request must be chunked or have a content length.
答案 0 :(得分:0)
我在标题中添加了“ Content-Length:0”。仅在将cUrl与PUT一起使用时才有必要。
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'MerchantId: ' . $this->merchant_id, 'MerchantKey: ' . $this->merchant_key, 'Content-Length: 0' ));