微信官方PHP接口无法获得postStr数据

来源:互联网 发布:mac中显示隐藏文件夹 编辑:程序博客网 时间:2024/04/24 01:35

在微信官方提供的PHP接口文档中,处理主要业务有这么一段代码

public function getIndex(){        if (ob_get_contents())  ob_end_clean();        $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"])?$GLOBALS["HTTP_RAW_POST_DATA"]:'';//获取post过来的数据        if (!empty($postStr)){            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);            $this->postObj = $postObj;            $this->fromUserName = $postObj->FromUserName;//发送方            $this->toUserName = $postObj->ToUserName;//接收方(即本公众账号)            $this->msgType = trim($postObj->MsgType);//消息类型            $this->content = trim($postObj->Content);            $this->event = trim($postObj->Event);            $this->eventKey = trim($postObj->EventKey);            $this->dealMsg();        }else{            $this->wx->makeInfo(Input::all());            $this->wx->valid();//非消息推送时,默认为校验        }        flush();    }

这个方法中通过GLOBALS这个全局变量来获取原始的Post数据,但是这个方法也同时存在问题。

要通过Globals来获取数据,php.ini中的register_globals必须设置为ON,在低版本的php中register数据默认为开启,但是高版本中,默认为关闭状态,这是因为,设置为ON后,表单提交过来的变量,会自动变成变量,也就是说
在地址栏输入的变量,都变成全局变量,这个是非常不安全的。

除了$GLOBALS["HTTP_RAW_POST_DATA"]获取post来的数据,也可以通过file_get_contents("php://input")方法来获取post来的数据,

原创粉丝点击