2

我想在 Curl Php 中以百分比编码格式发布我的数据。我发布了一个数据名称=“测试测试测试”;我希望将其编码为 test%20test%20test,但它将数据发布为 test+test+test。

在传入 curl 之前,我也将 rawurlencode 方法设置为百分比编码,但 curl 将其转换为正常编码。

请让我知道我错过了什么?

//creating my post parameter


 $params = array();  



 foreach ($array as $key => $value) {
$params[] = $key . '=' . rawurlencode($value);

  }

return implode('&', $params);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch);
4

1 回答 1

2

POST 数据应该被编码为application/x-www-form-urlencoded. 在这种编码中,空格表示为+,而不是 %20 并且您得到的结果 ( test+test+test) 是正确的。见http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

这就是为什么curl翻译%20+. PHP cURL 库仅支持 POSTing in application/x-www-form-urlencodedmultipart/form-dataMIME 类型。

于 2013-05-08T08:07:29.190 回答