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

后端 未结 12 2177
遥遥无期
遥遥无期 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:30

    As noted elsewhere on the web, php://input content does not get through if request is redirected. Maybe your web server has a redirect from an URL starting with www to an URL without www, or from URLs using HTTP to URLs using HTTPS. Check your web server logs to verify this, and act accordingly to avoid the redirection when making calls to the web service.

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

    I had the same problem. Eventually, I found that the problem was that in the client side I didn't stringify the json object (previously I used nodejs server and it was OK). Once I did that, I received the data in $_POST (not as json), as regular parameters.

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

    I wrote a headerfile with this code:

    if($_SERVER["REQUEST_METHOD"] == "POST" && $_SERVER["CONTENT_TYPE"] == "application/json")
    {
      $data = file_get_contents("php://input", false, stream_context_get_default(), 0, $_SERVER["CONTENT_LENGTH"]);
      global $_POST_JSON;
      $_POST_JSON = json_decode($_REQUEST["JSON_RAW"],true);
    
      // merge JSON-Content to $_REQUEST 
      if(is_array($_POST_JSON)) $_REQUEST   = $_POST_JSON+$_REQUEST;
    }
    

    It checks for the correct content-type and it reads only as much post input, like specified in Content-Length header. When receiving a valid JSON it created an global Array $_POST_JSON.

    So you can work with your JSON-Content the similiar like you do it with url-encoded POST values.

    Example:

     echo $_POST_JSON["address"];
     // or
     echo $_REQUEST["address"];
    
    0 讨论(0)
  • 2020-12-09 09:40

    for me this problem started, suddenly.
    i had installed ssl certificate.
    when i checked the response code it showed me 301, then i realized and changed
    http://
    to
    https://
    and got all the request back with file_get_contents('php://input').

    in your example curl call:
    put s after http as shown below:
    curl -i -X PUT -d '{"address":"Sunset Boulevard"}' https://localhost/clients/ryan

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

    First off, i was able to run this code and it worked fine:

    --Terminal---------
    //I ran this curl request against my own php file:
    curl -i -X PUT -d '{"address":"Sunset Boulevard"}' http://localhost/test.php
    
    
    --PHP--------------
    //get the data
    $json = file_get_contents("php://input");
    
    //convert the string of data to an array
    $data = json_decode($json, true);
    
    //output the array in the response of the curl request
    print_r($data);
    

    If that doesn't work, check the console for errors and your php settings:

    1. the curl url you used, make sure that url is actually working and not returning errors.
    2. open up another terminal / console window and run tail -f /path/to/the/php/log/file so you can actually see the output of these php calls.
    3. often people get this error: file_get_contents(file://input): failed to open stream: no suitable wrapper could be found which can indicate either a typo of the "file://input" string or the fact that allow_url_fopen is disabled in php (see #5 if unsure)
    4. make sure your code is correct, and by that I mean make sure you're not typing in incorrect arguments and things... stuff that doesn't necessarily get underlined in netbeans.
    5. remember, file_get_contents only works when allow_url_fopen is set to true in your PHP settings. thats something that is set in php.ini, but you can also change settings at run time by writing something along the lines of the following code before the other code:

      ini_set("allow_url_fopen", true);
      
    0 讨论(0)
  • 2020-12-09 09:45

    The correct function call is:

    file_get_contents("php://input");
    

    You should be getting an error message saying that php:input doesn't exist...

    In any case, PUT is intended for uploading files to the server, assuming the server supports it. Usually you'd use POST with a Content-Type header appropriate to the content.

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