首页 > 解决方案 > POST JSON MULTIDIMENSIONAL PHP 不工作

问题描述

我尝试创建 post curl json 数组多维,但结果为NULL

我不知道为什么不工作。这是我的代码

$d = array('code' => 1111, 'number' => "08813812");
$array = array('content' => $d);
$result =  json_encode($d);
// echo $result;

$data = array( 
    'content' => $d,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/receive-callback.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "cache-control: no-cache",
    "postman-token: 701ab5b6-002e-a982-0cfd-c6eee34ae7e1",
    "Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo $response;

这是为了接收帖子

$d = $_POST['content'];
// file_put_contents('hehe.txt', $d);
print(json_encode($d, JSON_PRETTY_PRINT));
// $ho = json_decode($d);
// echo $d['code'];

在这种情况下,我希望接收器中的结果是来自对象数组内容的值

标签: phparraysjson

解决方案


使用以下内容更改您的代码:-

$d = array('code' => 1111, 'number' => "08813812");
$array = array('content' => $d);
$result =  json_encode($d);
// echo $result;

$data = array( 
    'content' => $d
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/stackoverflow/receive-callback.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "cache-control: no-cache",
    "postman-token: 701ab5b6-002e-a982-0cfd-c6eee34ae7e1",
    "Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
echo $response;

要接收使用以下内容:-

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);

推荐阅读