【问题标题】:PHP curl is not parsing POST dataPHP curl 不解析 POST 数据
【发布时间】: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 其余为空

标签: php post curl https xampp


【解决方案1】:

您不应该对整个参数字符串进行 URL 编码,这将对 =&amp; 进行编码,因此它们不再是分隔符。您应该只对值进行编码。

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 格式发送它。

【讨论】:

  • 我刚刚更改了它,遗憾的是我的请求仍然是空的。我不知道为什么我的请求是空的。注意:我将我的代码编辑为更新的代码。
  • 请把问题放回去。如果他们看不到您问题中的原始代码,他们怎么能理解我回答的原因?
  • 尝试去掉 Content-type 标头,这不是 cURL 对数组进行编码的方式。
  • 我把它拿出来了,但我的请求中仍然没有 POST/HEAD 数据。我也会改回我原来的帖子,这样人们就会明白你的答案!
  • 尝试去掉所有的标题——它们似乎都没有必要。
猜你喜欢
  • 2016-12-10
  • 1970-01-01
  • 2018-07-23
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
相关资源
最近更新 更多