Reading json input in php

后端 未结 2 1734
一整个雨季
一整个雨季 2020-12-11 03:40

php://input is working properly in localhost. But in server it returns empty. Input( request ) to my site is a json(REST - application/json typ

相关标签:
2条回答
  • 2020-12-11 04:08

    I know this is old, but it might help others:

    Careful with single quotes inside your json

    From the PHP documentation about json_decode:

    the name and value must be enclosed in double quotes
    single quotes are not valid 
    
    $bad_json = "{ 'bar': 'baz' }";
    json_decode($bad_json); // null
    
    0 讨论(0)
  • 2020-12-11 04:10

    php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

    See wrappers

    Try this too,

    ....
      public function __construct(){
        parent::__construct();
        if(isset($_POST))
        {
           var_dump(file_get_contents('php://input'));
           $this->json_input_data=json_decode(file_get_contents('php://input'),TRUE);
        }
        else echo 'Not Post';
      }
    ....
    

    Also check for allow_url_fopen.

    0 讨论(0)
提交回复
热议问题