🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1.微信token接入验证 ~~~ protected function valid() { $echoStr = $_GET['echostr']; //通过GET方法获取echostr的值 if ($this->checksSignature()) { echo $echoStr; //输出echoStr exit; } } ~~~ 2.签名 ~~~ protected function checksSignature() { //signature 是微信传过来的 类似于签名的东西 $signature = $_GET["signature"]; //微信发过来的东西 $timestamp = $_GET["timestamp"]; //微信传过来的值 什么用我不知道... $nonce = $_GET["nonce"]; //定义你在微信公众号开发者模式里面定义的token $token = "weixin"; //三个变量 按照字典排序 形成一个数组 $tmpArr = array( $token, $timestamp, $nonce ); sort($tmpArr, SORT_STRING); $tmpStr = implode($tmpArr); //哈希加密 在laravel里面是Hash:: $tmpStr = sha1($tmpStr); //按照微信的套路 给你一个signature没用是不可能的 这里就用得上了 if ($tmpStr == $signature) { return true; } else { return false; } } ~~~ 3.文本消息回复 ~~~ public function response_text($fromUsername, $toUsername) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>%s</ArticleCount> %s </xml>"; $count=1; $str="<Articles> <item> <Title>< ![CDATA[壹城好店!] ]></Title> <Description>< ![CDATA[同城美食,家居,时尚,健身,教育.....咨询平台] ]></Description> <PicUrl>< ![CDATA[http://admin.nineopen.com:8000/uploads/banner/20180708/5bdd6e995e52d665c6486529c1bf8188.jpg] ]></PicUrl> <Url>< ![CDATA[http://www.nineopen.com] ]></Url> </item> </Articles>"; $msgType = "news"; $time =time(); $resultStr = sprintf($textTpl,$toUsername,$fromUsername, $time, $msgType,$count,$str); echo $resultStr; } ~~~ 4.通过菜单点击事件获取openid ~~~ protected function get_openid() { $postStr = file_get_contents('php://input'); libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $MsgType =trim($postObj->MsgType); $FromUserName =trim($postObj->FromUserName); $ToUserName =trim($postObj->ToUserName); $Event =trim($postObj->Event); if ($MsgType == 'event' and $Event == 'subscribe') { return $FromUserName; } } ~~~ 5.创建菜单 ~~~ public function menu(){ $token =$this->gettoken(); $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$token; $jsondata = array("button" => array(['type' => 'view', 'name' => '微商城', 'url' => 'https://tuan.nineopen.com'], ['type' => 'view', 'name' => '个人中心', 'url' => 'https://tuan.nineopen.com/personal'])); $data = json_encode($jsondata, JSON_UNESCAPED_UNICODE); $obj= $this->curl($url,$data); return $obj; } ~~~ 6.检查菜单 ~~~ public function menucheck(){ $token =$this->gettoken(); $url = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$token; $obj= $this->curl_get($url); return $obj; } ~~~