doudan4834 2014-06-11 04:10
浏览 98

如何在Apache 2.2上使用PHP 5.4从PUT请求获取数据

I've tried all of them methods on could find that have worked for other folks, but with no luck.

Here's my PHP code:

parse_str(file_get_contents("php://input"), $put_data);
echo "foo is: " . $put_data['foo'] . "
";

No matter what I try, I get 'foo is: '

I'm thinking this is specific to Apache as running the same script on an nginx server shows the expected 'foo is: bar'

Things I've tried with no change in the result:

Changing my Content-Type header to application/x-www-form-urlencoded.

Adding a LimitExcept directive to my apache config, like so (yes, I restarted after making the change)

<Directory "/MyApp/Directory/">
    Allow From All
    AllowOverride All
    <LimitExcept GET POST PUT OPTIONS DELETE>
        Deny from All
    </LimitExcept>
</Directory>

Using stream_get_contents like so:

$put_data = fopen("php://input", "r");
$data = stream_get_contents($put_data);
echo "data is: " . $data;
fclose($put_data);

Here are the response headers:

Date: Wed, 11 Jun 2014 04:03:09 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.24
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 7
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8

Thanks to any who can offer help. Pulling what's left of my hair out on this one for hours now.

  • 写回答

1条回答 默认 最新

  • dousu1900 2014-06-24 08:34
    关注

    I've tested your code and it's working fine on my setup which is very similar to yours. The only thing I can think of is this:

    Note: A stream opened with php://input can only be read once; the stream does not support seek operations.

    From: http://www.php.net/manual/en/wrappers.php.php

    Try and find a place in your code which is already reading 'php://input'. If you're using a framework of some sort, this would be my guess. I would search the entire project files for "php://input" to find out.

    If that doesn't seem to be the case, please pastebin the results of the following:

    print_r(file_get_contents("php://input"));exit;
    
    var_dump(file_get_contents("php://input"));exit;
    

    P.S. The rest of the note from the PHP manual website reads:

    However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.

    The SAPI implementation could be the difference between your nginx and Apache behaviours.

    评论

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了