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

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

问题描述

我正在构建一个 PHP RESTful API,遵循 this 教程.下面的函数应该在使用'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:

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?

推荐答案

As 在网络上的其他地方提到,如果请求被重定向,php://input 内容不会通过.也许您的 Web 服务器已从以 www 开头的 URL 重定向到不带 www 的 URL,或者从使用 HTTP 的 URL 重定向到使用 HTTPS 的 URL.检查您的网络服务器日志以验证这一点,并采取相应措施避免在调用网络服务时发生重定向.

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.

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

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