file_get_contents('php://input') always returns an empty string

后端 未结 12 2153
遥遥无期
遥遥无期 2020-12-09 09:04

I\'m building a PHP RESTful API, following this tutorial. The following function, which should return the data sent with the request when the \'put\' method is used, returns

相关标签:
12条回答
  • 2020-12-09 09:45

    Edit as you wish

    function getPostObject() {
        $str = file_get_contents('php://input');
        $std = json_decode($str);
        if ($std === null) {
            $std = new stdClass();
            $array = explode('&', $str);
            foreach ($array as $parm) {
                $parts = explode('=', $parm);
                if(sizeof($parts) != 2){
                    continue;
                }
                $key = $parts[0];
                $value = $parts[1];
                if ($key === NULL) {
                    continue;
                }
                if (is_string($key)) {
                    $key = urldecode($key);
                } else {
                    continue;
                }
                if (is_bool($value)) {
                    $value = boolval($value);
                } else if (is_numeric($value)) {
                    $value += 0;
                } else if (is_string($value)) {
                    if (empty($value)) {
                        $value = null;
                    } else {
                        $lower = strtolower($value);
                        if ($lower === 'true') {
                            $value = true;
                        } else if ($lower === 'false') {
                            $value = false;
                        } else if ($lower === 'null') {
                            $value = null;
                        } else {
                            $value = urldecode($value);
                        }
                    }
                } else if (is_array($value)) {
                    // value is an array
                } else if (is_object($value)) {
                    // value is an object
                }
                $std->$key = $value;
            }
            // length of post array
            //$std->length = sizeof($array);
        }
        return $std;
    }
    
    0 讨论(0)
  • 2020-12-09 09:47

    I had got this error too, my solution is to change the data format to raw.

    I get it from php doc where it says

    php://input is not available with enctype="multipart/form-data".

    0 讨论(0)
  • 2020-12-09 09:52

    I see the problem,

    You need to add filename.php in the end of the request url or need to rewrite the server rules in .htaccess file to get around this.

    curl -i -X PUT -d '{"address":"Sunset Boulevard"}' http://localhost/clients/ryan/{filename.php}
    

    replace {filename.php} with appropriate file name. :)

    0 讨论(0)
  • 2020-12-09 09:53

    Make sure there are no redirects.

    file_get_contents(php://input) doesn't pass the body content through redirects.

    One quick way to check for redirects is to check the url with curl. On the command line: curl -IL http://example.com/api

    0 讨论(0)
  • 2020-12-09 09:53
    {
      "action":"get_events_by_category",
      "category_id":2
    }
    

    Your row string data must be a Double quote "" not use single ''

    0 讨论(0)
  • 2020-12-09 09:55

    On Windows the combination of 'single "double" quotes' does not seem to work. Use escape for quotes in your json data (as below) & it should work

    curl -X PUT -d "{\"address\":\"Sunset Boulevard\"}" http://localhost/clients/ryan
    
    0 讨论(0)
提交回复
热议问题