php 开发 微信公众号报错 Undefined index: HTTP_RAW_POST_DATA

 出现错误原因:

               php 7以下版本,php.ini开启;always_populate_raw_post_data = On 

               PHP 7以后的版本不支持全局变量:always_populate_raw_post_data,所以会报错,解决办法:把下面代码

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"] 

修改为:

$postStr = file_get_contents("php://input");

顺便贴上完整代码供参考: 

public function index()
    {
        $timestamp = $_GET['timestamp'];//timestamp其实就是一个时间戳
        $nonce     = $_GET['nonce'];//nonce是一个随机参数
        $token     = "公众号后台配置的token";//这个token填写你在微信公众平台上写的那个值
        $signature = $_GET['signature'];//这个signature其实就是在微信公众平台已经加密好的字符串
        $echostr   = $_GET['echostr'] ?? '';
        $array     = array($timestamp, $nonce, $token);
        sort($array);
        $tmpstr = implode('', $array);
        $tmpstr = sha1($tmpstr);
        if ($tmpstr == $signature && $echostr) {
            ob_clean();
            echo $echostr;
            exit;
        } else {
            $this->responseMsg();

        }
    }
    public function responseMsg()
    {
        //get post data, May be due to the different environments

        $postStr = file_get_contents("php://input"); //php 7 以上版本
        //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //php 7以下版本,php.ini开启;always_populate_raw_post_data = On

        //extract post data 请求数据
        if (!empty($postStr)) {

            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $keyword = trim($postObj->Content);
            $time    = time();
            $toUser   = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $eventKey = $postObj->EventKey ?? '';
            $ticket   = $postObj->Ticket ?? '';
            if (strtolower($postObj->MsgType) == 'event') {
                //如果是关注事件(subscribe)
                if (strtolower($postObj->Event == 'subscribe')) {
                    //回复用户消息
                    $data     = [
                        'open_id'   => $toUser,
                        'event_key' => $eventKey,
                        'ticket'    => $ticket,
                    ];
                    //关注数据写入数据库
                    list($status,$res) = GzhFollow::updateGzhFollowInfo($data);
                    if(!$status){
                        ownLogs('test.log', $res);
                    }
//                    ownLogs('test.log', 'fromUser=' . $fromUser . '|toUser=' . $toUser . '|keyword=' . $keyword);

                }
                //取消关注事件(subscribe)
                if (strtolower($postObj->Event == 'unsubscribe')) {
                    //更新GzhFollow表status为取消关注
                    $open_id = $toUser;
                    $info = GzhFollow::getFollowInfoByOpenId($open_id);
                    if($info){
                        $info->status = GzhFollow::STATUS['off'];
                        $info->save();
                    }
                }
                //扫描带参数二维码事件
                if($eventKey && $ticket){
                    $msgType     = 'text';
                    $store_name  = '李伟';//测试数据先写死
                    $description = '【' . $store_name . "】的店铺!";
                    //文字消息处理成超链接(因为我现在需求要跳转到小程序)
                    $content     = '点击进入' . '' . $description . '';
                    $template    = "
                                  
                                  
                                  %s
                                  
                                  
                                ";

                    $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
                    echo $info;
                }
            }

        } else {
            echo "";
            exit;
        }
    }

 

你可能感兴趣的:(微信周边,PHP,laravel,php,公众号,HTTP_RAW_POST)