【发布时间】:2016-02-07 01:51:01
【问题描述】:
所以我正在尝试向网站发送 HTTPS POST 请求,但 curl 没有设置我的帖子数据或标题。 这是我的代码(我更改了域和值):
<?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 数据或标头。
这是我在提琴手中的要求:
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 数据和标头。我不知道为什么 curl 没有设置我的标题和我的帖子字段。
信息:我正在使用 XAMPP 和 php5。
【问题讨论】:
-
尝试将
DATA1从您的 CURL 请求中取出,看看您是否得到相同的响应。 -
您可以控制您发布到的服务器吗?如果是这样,你能发出
print_r($_POST);吗? -
你为什么要设置
$headers两次? -
DATA1 和 DATA2 实际上是必需的,并且具有我需要发送的实际值。如果我要删除它们,我仍然会收到错误的请求,因为它们的值是必需的 正常请求如下所示: POST website.com/test HTTP/1.1 User-Agent: MuUserAgent Content-Type: application/x-www-form-urlencoded东西:Something_Data 主机:website.come 内容长度:205 DATA1=VAL1&DATA2=VAL2 我会得到这个答案:HTTP/1.1 200 OK(不包括一些...字符限制){“结果”:“这里的结果”}
-
遗憾的是,我无法控制要发布到的服务器,但我使用 fiddler 检查数据包,并且我的标头中只有 2 件事。 POST website.com/test 主机:website.com 其余为空