我将数组编码为json并通过curl将其发送到其他文件:
$data = array(
"authorizedKey" => "abbad35c5c01-xxxx-xxx",
"senderEmail" => "myemail@yahoo.com",
"recipientEmail" => "jaketalledo86@yahoo.com",
"comment" => "Invitation",
"forceDebitCard" => "false"
);
$data = json_encode($data);
$ch = curl_init('http://localhost/curl/1.php');
$headers = array('Accept: application/json','Content-Type: application/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo curl_exec($ch);
curl_close($ch);
问题是http://localhost/curl/1.php没有收到任何$_POST
,我也不知道如何对发布的数据进行操作。
http://localhost/curl/1.php输出:
Headers:
Host = localhost
Accept = application/json
Content-Type = application/json
Content-Length = 166
GET:
empty
POST:
empty
答案 0 :(得分:1)
您正在使用请求正文发送数据,并且您应该通过请求正文获取数据而不是$ _POST super global。您可以使用以下代码来获取整个数据。
$entityBody = file_get_contents('php://input');
答案 1 :(得分:1)
如果您希望数据显示在$_POST
中,请不要将其编码为JSON。只需将$data
数组传递给CURLOPT_POSTFIELDS
选项,就不要发送Content-type: application/json
标题。然后,cURL
将使用POST
数据的常规编码,PHP可以将其转换为$_POST
变量中的字段。