解决升级PHP7后 微信公众号收不到消息

服务器配置Linux+Nginx+PHP5.5+mysql

index方法配置微信的关注回复、菜单事件、多客服、自动回复等

    public function actionIndex() {
        if (isset($_GET["echostr"]) && !empty($_GET["echostr"])) {
            $this->valid();
        } else {
            $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : '';
            if (empty($postStr)) {
                exit('.');
            }
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            foreach ($postObj as $key => $value) {
                $this->data[$key] = strval($value);
            }
            /**
             * 关注时回复
             */
            $arr_data = $this->data;
            if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'subscribe') {  // 关注时回复
                /**
                 * 场景二维码
                 */
                $qr = explode('_', $arr_data['EventKey']);
                if ($arr_data['EventKey'] && isset($qr[1])) {
                    /**
                     * 场景二维码,首次关注
                     */
                    $this->sceneQrSubscribe($qr[1], $arr_data['FromUserName']);
                    $this->sceneQr($qr[1], $arr_data['FromUserName']);
                }
                $WxSubscribeText = commonmodelsWxSubscribeText::find()->one();
                $SubscribeText = 'msg';
                if (!empty($WxSubscribeText)) {
                    $SubscribeText = $WxSubscribeText->text;
                }
                $this->response($SubscribeText);
            } else if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'LOCATION') {  //地理位置消息
            } elseif ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'CLICK') {  // 事件, 点击自定义菜单    
                $this->event($arr_data);
            } else if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'SCAN') {    // 再次关注 
                if (isset($arr_data['EventKey']) && $arr_data['EventKey']) {
                    $this->sceneQr($arr_data['EventKey'], $arr_data['FromUserName']);
                }
                $this->response('您已经关注我们了!'); //"扫描 ".$arr_data['EventKey'];
            } else if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'unsubscribe') {  // 取消关注事件
                $this->unsubscribe($arr_data['FromUserName']);
                // 取消关注事件
            } else { //  文本输入  检查 发送图片
               
                /**
                 * 多客服
                 */
                $this->response('没有找到您要的内容!', 'text_kefu');
            }
        }
    }

今天升级系统PHP版本为PHP7后,其他页面访问,后台、前台页面都没有问题,唯有访问微信公众号API接口提示“该公众号无法提供服务,请稍后重试”

后来在网上搜索,在OpenStack社区找到了回答,PHP7抛弃了HTTP_RAW_POST_DATA,要获取post数据可以用

php://input代替

修改代码为:

 public function actionIndex() {
        if (isset($_GET["echostr"]) && !empty($_GET["echostr"])) {
            $this->valid();
        } else {
            $postStr =file_get_contents("php://input");
            if (empty($postStr)) {
                exit('.');
            }
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            foreach ($postObj as $key => $value) {
                $this->data[$key] = strval($value);
            }
            /**
             * 关注时回复
             */
            $arr_data = $this->data;
            if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'subscribe') {  // 关注时回复
                /**
                 * 场景二维码
                 */
                $qr = explode('_', $arr_data['EventKey']);
                if ($arr_data['EventKey'] && isset($qr[1])) {
                    /**
                     * 场景二维码,首次关注
                     */
                    $this->sceneQrSubscribe($qr[1], $arr_data['FromUserName']);
                    $this->sceneQr($qr[1], $arr_data['FromUserName']);
                }
                $WxSubscribeText = commonmodelsWxSubscribeText::find()->one();
                $SubscribeText = 'msg';
                if (!empty($WxSubscribeText)) {
                    $SubscribeText = $WxSubscribeText->text;
                }
                $this->response($SubscribeText);
            } else if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'LOCATION') {  //地理位置消息
            } elseif ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'CLICK') {  // 事件, 点击自定义菜单    
                $this->event($arr_data);
            } else if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'SCAN') {    // 再次关注 
                if (isset($arr_data['EventKey']) && $arr_data['EventKey']) {
                    $this->sceneQr($arr_data['EventKey'], $arr_data['FromUserName']);
                }
                $this->response('您已经关注我们了!'); //"扫描 ".$arr_data['EventKey'];
            } else if ($arr_data['MsgType'] == 'event' && $arr_data['Event'] == 'unsubscribe') {  // 取消关注事件
                $this->unsubscribe($arr_data['FromUserName']);
                // 取消关注事件
            } else { //  文本输入  检查 发送图片
               
                /**
                 * 多客服
                 */
                $this->response('没有找到您要的内容!', 'text_kefu');
            }
        }
    }

一切OK了!