PHP curl 好像在剥离post body
PHP curl seems to be stripping post body
我有一个 PHP curl 语句,由于某种原因发送时格式错误。不管怎样,用POST方式发出来的body是空的。它只发送 headers。我似乎无法弄清楚为什么。
我正在 JSON 编码调用此函数的 $data
有效载荷。
(php5.6 / 阿帕奇)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-xx-MerchantId: ' . $this->opt['merchantId'],
'X-xx-REST-API-Key: ' . $this->opt['apiKey'],
'Content-Type: application/json',
'Accept: application/json'
));
if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
elseif ($method == "GET") {
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
}
elseif ($method == "PUT") {
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
}
elseif ($method == "DELETE") {
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "PHPxx v2");
$result_text = curl_exec($ch);
$curl_request_info = curl_getinfo($ch);
curl_close($ch);
使用测试工具进行 API 调用 我已经验证了 curl 发送的内容显示空白 body。
在针对真实端点进行测试时,端点 returns 一个似乎暗示没有 body 的错误。 (411 状态代码)
curl_getinfo($ch) 的转储:
这看起来像你 API KEY 后面有两个新行。两个换行符表示 "End Of Header, Start the content"。所以你把可怜的东西弄糊涂了。
您的第一次调试显示 "Content-Type" 和 "Accept" headers 没有正确执行。
您的第二次调试显示相同的两个 headers 未被使用;显示两个换行符,但也显示您正在传递一些内容。
去掉那些换行符,你应该是对的。
我有一个 PHP curl 语句,由于某种原因发送时格式错误。不管怎样,用POST方式发出来的body是空的。它只发送 headers。我似乎无法弄清楚为什么。
我正在 JSON 编码调用此函数的 $data
有效载荷。
(php5.6 / 阿帕奇)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-xx-MerchantId: ' . $this->opt['merchantId'],
'X-xx-REST-API-Key: ' . $this->opt['apiKey'],
'Content-Type: application/json',
'Accept: application/json'
));
if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
elseif ($method == "GET") {
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
}
elseif ($method == "PUT") {
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
}
elseif ($method == "DELETE") {
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "PHPxx v2");
$result_text = curl_exec($ch);
$curl_request_info = curl_getinfo($ch);
curl_close($ch);
使用测试工具进行 API 调用 我已经验证了 curl 发送的内容显示空白 body。
在针对真实端点进行测试时,端点 returns 一个似乎暗示没有 body 的错误。 (411 状态代码)
curl_getinfo($ch) 的转储:
这看起来像你 API KEY 后面有两个新行。两个换行符表示 "End Of Header, Start the content"。所以你把可怜的东西弄糊涂了。
您的第一次调试显示 "Content-Type" 和 "Accept" headers 没有正确执行。
您的第二次调试显示相同的两个 headers 未被使用;显示两个换行符,但也显示您正在传递一些内容。
去掉那些换行符,你应该是对的。