最近在Apache的error.log里出现一个E_DEPRECATED,PHP Deprecated:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

        里面提到在php.ini将always_populate_raw_post_data设为-1可修复。按建议来做,重启Apache,再运行,却再出现错误。

        PHP Notice:  Undefined index: HTTP_RAW_POST_DATA in ***xin\\index.php on line 43

        查看对应的line 43,发现此句用了$GLOBALS["HTTP_RAW_POST_DATA"],而将always_populate_raw_post_data设为-1后,则强制 $HTTP_RAW_POST_DATA 未定义。因为根据之前的提示HTTP_RAW_POST_DATA可能在后续的 PHP 版本中被移除,需要用获取php://input的文件流来代替。于是将$GLOBALS["HTTP_RAW_POST_DATA"]改为file_get_contents('php://input')。

        重新再运行,一切正常啦。

        伴随着这个Deprecated的还有个Warning:PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0。不过弄好这个Deprecated后这个Warning也消失了。猜想是和$HTTP_RAW_POST_DATA有关,不过根据Warning的意思,它是怎样被拦截不能修复头信息的呢?


        百度到的出现此Warning是这样子的:PHP脚本开始执行时,它可以同时发送header(标题)信息和主体信息。 Header信息(来自 header() 或 SetCookie() 函数)并不会立即发送,相反,它被保存到一个列表中。 这样就可以允许你修改标题信息,包括缺省的标题(例如 Content-Type 标题)。但是,一旦脚本发送了任何非标题的输出(例如,使用 HTML 或 print() 调用),那么PHP就必须先发送完所有的Header,然后终止 HTTP header。而后继续发送主体数据。从这时开始,任何添加或修改Header信息的试图都是不允许的,并会发送上述的错误消息之一。