[英]Content-Type application/json doesn't return POST fields
I'm making cURL request to file on my server And it's printing empty array, unless I delete Content-Type: application/json header from request. 我正在向服务器上的文件发出cURL请求,并且正在打印空数组,除非我从请求中删除Content-Type:application / json标头。 Why it occure?
为什么会发生? It should expect json format in return....
它应该期望返回json格式....
$o = [ 'key' => 'value123' ];
$headers[] = 'Content-Type: application/json';
$headers[] = 'Moj-pierwszy-header: prosty/do/zapamietania';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path.'test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $o);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo( $output );
and the test.php 和test.php
echo json_encode( $_POST );
Anybody? 任何人? Any ideas?
有任何想法吗?
Please refer the below code snippet: 请参考以下代码段:
$o = json_encode([ 'key' => 'value123' ]);
$headers[] = 'Content-Type: application/json';
$headers[] = 'Moj-pierwszy-header: prosty/do/zapamietania';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path.'test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $o);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo( $output );
and for output, instead of $_POST use below code snippet: 对于输出,而不是$ _POST使用下面的代码片段:
$post = json_decode(file_get_contents('php://input'));
$ post = json_decode(file_get_contents('php:// input'));
Hope, it'll work for you. 希望对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.