php - 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'];
在这种情况下,我希望接收器中的结果是来自对象数组内容的值
解决方案
使用以下内容更改您的代码:-
$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);
推荐阅读
- html - 面包屑项目的边框着色
- batch-file - Windows 批处理循环变量以
- asp.net-web-api - 空请求正文拒绝请求
- html - 如何为多个背景图像设置动画?
- asp.net-mvc - 当我在缓存控制中保持无缓存、无存储时,Internet Explorer 中未加载图标
- python - Pandas Dataframe 按列值拆分为单独的 csv
- mysql - 如何将 HubSpot 数据自动集成到 MySQL 数据库中?
- arrays - 动态分配内存给数组并读取大文本文件
- c - 如何实现环境 | 带有 fork execve 和管道的 grep LANG
- node.js - npm 如何检查软件包版本是否易受攻击并在命令提示符中显示?