简体   繁体   English

内容类型application / json不返回POST字段

[英]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.

相关问题 在脚本中设置 application/json 内容类型不起作用 - Setting application/json content-type in script doesn't work Ajax中的JSON字符串发布请求内容类型application / formdata与application / json - JSON string in ajax post request content-type application/formdata vs application/json 应该什么时候Content-Type是application / json - When should Content-Type be application/json 无法将其转换为content-type:application / json - can not convert it into content-type:application/json `header(“Content-type:application / json”)的用法;` - The usage of `header(“Content-type:application/json”);` curl -X POST -H'Content-Type:application / json' -d to PHP - curl -X POST -H 'Content-Type: application/json' -d to PHP 如果标题中包含“ Content-Type:application / json”,则不会创建PHP 5.4.9 $ _POST超全局变量 - PHP 5.4.9 $_POST superglobal not created if 'Content-Type: application/json' is in header 如何将'content-type application / json'的数据发布到苗条的php rest服务 - How to post data of 'content-type application/json' to slim php rest service 如何在CakePHP中为JSON返回正确的内容类型? - How to return the correct content-type for JSON in CakePHP? 如何在此请求中添加Content-Type application / json - How to add Content-Type application/json in this request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM