PHP curl 未解析 POST 数据
PHP curl is not parsing POST data
所以我正在尝试向网站发送 HTTPS POST 请求,但 curl 没有设置我的 post 数据,也没有设置我的 headers。
这是我的代码(我更改了域和值):
<?php
//Create connection
$ch = curl_init();
//Set the headers
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
//Set the POST data
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
// Configure the request (HTTPS)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, "https://website.com/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "MyUserAgentHere");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1); //just to be sure...
//Set the POST data and Headers
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("DATA1=VAL1&DATA2=VAL2"));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
echo $result;
curl_close($ch);
?>
所以基本上我正在尝试向 https://website.com/test" 发送 HTTPS POST 请求。请求已发送,我得到了答复,但由于某些原因它没有解析 POST 数据和 Headers。
这是我在 fiddler 中的请求:
POST https://website.com/test HTTP/1.1
Host: website.com
这是请求的答案:
HTTP/1.1 400 Bad Request
Date: Fri, 06 Nov 2015 00:57:15 GMT
Server: Apache
Cache-Control: no-store
Pragma: no-cache
Connection: close
Content-Type: application/json;charset=UTF-8
Content-Length: 158
{"error":"unsupported DATA1"}
如您所见,由于某种原因,POST 数据和 headers 未在我的请求中设置。我不知道为什么 curl 没有设置我的 headers 和 post 字段。
信息:我正在使用 XAMPP 和 php5。
您不应该对整个参数字符串进行 URL 编码,这将对 =
和 &
进行编码,因此它们不再是分隔符。您应该只对值进行编码。
curl_setopt($ch, CURLOPT_POSTFIELDS, 'DATA1=' . urlencode($val1) . '&DATA2' . urlencode($val2));
您可以改用数组,cURL 将为您进行编码。
curl_setopt($ch, CURLOPT_POSTFIELDS, array('DATA1' => $val1, 'DATA2' => $val2));
如果使用数组机制,去掉Content-type: application/x-www-form-urlencoded
头,因为cURL以multipart/form-data
格式发送。
所以我正在尝试向网站发送 HTTPS POST 请求,但 curl 没有设置我的 post 数据,也没有设置我的 headers。 这是我的代码(我更改了域和值):
<?php
//Create connection
$ch = curl_init();
//Set the headers
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
//Set the POST data
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
// Configure the request (HTTPS)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, "https://website.com/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "MyUserAgentHere");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1); //just to be sure...
//Set the POST data and Headers
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("DATA1=VAL1&DATA2=VAL2"));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
echo $result;
curl_close($ch);
?>
所以基本上我正在尝试向 https://website.com/test" 发送 HTTPS POST 请求。请求已发送,我得到了答复,但由于某些原因它没有解析 POST 数据和 Headers。
这是我在 fiddler 中的请求:
POST https://website.com/test HTTP/1.1
Host: website.com
这是请求的答案:
HTTP/1.1 400 Bad Request
Date: Fri, 06 Nov 2015 00:57:15 GMT
Server: Apache
Cache-Control: no-store
Pragma: no-cache
Connection: close
Content-Type: application/json;charset=UTF-8
Content-Length: 158
{"error":"unsupported DATA1"}
如您所见,由于某种原因,POST 数据和 headers 未在我的请求中设置。我不知道为什么 curl 没有设置我的 headers 和 post 字段。
信息:我正在使用 XAMPP 和 php5。
您不应该对整个参数字符串进行 URL 编码,这将对 =
和 &
进行编码,因此它们不再是分隔符。您应该只对值进行编码。
curl_setopt($ch, CURLOPT_POSTFIELDS, 'DATA1=' . urlencode($val1) . '&DATA2' . urlencode($val2));
您可以改用数组,cURL 将为您进行编码。
curl_setopt($ch, CURLOPT_POSTFIELDS, array('DATA1' => $val1, 'DATA2' => $val2));
如果使用数组机制,去掉Content-type: application/x-www-form-urlencoded
头,因为cURL以multipart/form-data
格式发送。