file_get_contents('php:// input')总是返回一个空字符串 [英] file_get_contents('php://input') always returns an empty string

查看:420
本文介绍了file_get_contents('php:// input')总是返回一个空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个PHP RESTful API,教程。以下函数应该在使用put方法时返回与请求一起发送的数据,每次都返回null:

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 null every time:

file_get_contents 'php:// input')

我已经下载并测试了教程中的完整代码示例,它仍然返回null。

I've even downloaded and tested the full code example in the tutorial and it still returns null.

我使用cURL和以下命令来测试'put'方法:

I'm using cURL and the following command in order to test the 'put' method:

code> curl -i -X PUT -d'{address:Sunset Boulevard}'http:// localhost / clients / ryan 。

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

我浪费了几天的时间,仍然没有读它的json数据。

I've wasted days on this and still haven't gotten it to read the json data. What am I doing wrong?

推荐答案

首先,我能够运行这个代码,并且工作正常:

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);

如果这不起作用,请检查控制台是否有错误和您的php设置:

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


  1. 您使用的curl网址,请确保网址实际上正常工作,而不会返回错误。

  2. 打开另一个终端/控制台窗口并运行 tail -f / path / to / the / php / log / file ,这样你可以看到这些php调用的输出。
  3. 经常有人得到这个错误: file_get_contents(file:// input):无法打开流:无法找到合适的包装器输入file:// input字符串或者在php中禁用 allow_url_fopen 的事实(如果不确定,请参见#5)

  4. 确保你的代码是正确的,我的意思是确保你不输入不正确的参数和东西...不一定在netbeans中加下划线的东西。

  5. 请记住, file_get_contents 仅适用于在您的PHP设置中将 allow_url_fopen 设置为true。这是在php.ini中设置的东西,但是你也可以在运行时通过在其他代码之前写下以下代码的行来更改设置:

  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);


这篇关于file_get_contents('php:// input')总是返回一个空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆