JSON 原始 POST cURL 请求不起作用



非常感谢任何帮助,提前感谢。

我的商店回调.php我托管的文件:http://xxxxx/storeCallback.php

<?php
die(var_dump($_REQUEST));

我正在创建一个后 JSON 原始请求:

1.

$url = "http://xxxxx/storeCallback.php/storeCallback.php";
$body =['foo' => 'bar'];
$body_string = json_encode($body);
$header = array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '.strlen($body_string),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body_string);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
die(var_dump($response));

阿拉伯数字。

$url = "http://xxxxx/storeCallback.php/storeCallback.php";
$body =['foo' => 'bar'];
$body_string = json_encode($body);
$header = array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '.strlen($body_string),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body_string);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
die(var_dump($response));

3.

<?php
require 'vendor/autoload.php';
use GuzzleHttpClient;
$client = new Client();
$response = $client->post('http://xxxxx/storeCallback.php/storeCallback.php', [
GuzzleHttpRequestOptions::JSON => ['for' => "bar"],
]);
echo $response->getBody()->getContents();

没有任何工作,在所有情况下响应都是 [](空数组(但像 http://xxxxx/storeCallback.php/storeCallback.php?foo=bar 这样的发送请求正在工作

您的代码有两个问题,首先storeCallback.php文件不提供有效的 JSON 输出,因此当您尝试使用 cURL 检索它时,您无法将其解析为 JSON。

正确的版本如下:存储回调.php

<?php
die(json_encode($_REQUEST));
?>

此外,您应该按如下方式发出 cURL 请求:

<?php
$url = "http://pc.medimetry.in/storeCallback.php";
$body = ['foo' => 'bar'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$json_response = curl_exec($ch);
curl_close($ch); //important, always close cURL connections.
$parsed_response = json_decode($json_response);
var_dump($parsed_response);
?>

(;

最新更新