? ?? ?? php WeChat?? ??? ?? ?? ??? ?? ??? ??? ?????. ?? ?? ???? ??? ? ????.
1. ??? ?? ??
??? ?? ??? ?? ??? ?????? ???? ?? ???? ?? ??? ??? ? ??? ? ? ??? ? ??? ?????. ?? ??? ?? ?? ?? ?????? ??? ????.
2. ?? ?? ??
?? ?? ?? Weibo ?? ? ?? ?? ??? ???? WeChat ??? ???? ?? ?? ??? ??? ? ????.
??? ???? ????? ?? ??? ????.
3. AppId ? AppSecert ??
AppId ? AppSecret? ??? ?? - ??? ID?? ??? ? ????.
4. ??? ?? ??
appid ? appsecert? ???? ??? ?? ??, ?????
api.weixin.qq.com/cgi-bi... mp;secret=APPSECRET
????? ??? ?? ?????
$appid = ""; $appsecret = ""; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); $access_token = $jsoninfo["access_token"];
???? ?? ?? ???? ??? ??? ? ?? ? ?? ???? ????.
??? ??? ????.
{"access_token":"N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g",
??? ??? ????.
"expires_in":7200}
???? ??? ??? ????
? ?
N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxO sVE08RfeD9lvK9lMguNG9kpIkKGZEj If8Jv2m9fFhf8bnNa-yQH3g
? ??? ?????.
?? ?? ????? ??? ??? ?????. ???
https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type???. = %E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95&form=%E8%87%AA%E5%AE%9A%E4%B9%89 % E8%8F%9C%E5%8D%95%E5%88%9B%E5%BB%BA%E6%8E%A5%E5%8F%A3%20/menu/create
? ?? ??? ?? ??? ??? ?? ?? ?????
???? ??? ????
?? ???? ??? ?? ????
5. ?? ??? ??
?? ??? ?? ???? ?? 3?? 1?? ??? ????, ? 1?? ???? ?? 5?? ??? ?????. 2? ??. 1?? ???? ?? 4?? ??? ??? ? ???, 2?? ???? ?? 7?? ??? ??? ? ????. ?? ??? "..."?? ?????. WeChat ????? ???? ?? ??? ?? ??? ??? ? ???? ? 24??? ????? ?? ?????. ????? ? ?? ??? ???? ?? ? ?? ???? ??? ??? ?? ? ??? ????? ?? ??????.
?? ??? ?? ?? ?????? ??? ?? ? ?? ??? ??? ??? ? ????.
??:
??? ??? ??? ???? WeChat ??? ??? ?????? ?? ????? ??? ?? ??? ??? ????(??? ????? ??? ??), ???? ??? ??? ? ?? ?? ???? ????? ? ????.
??: ???? ?? ?? ??? ???? WeChat ?????? ????. ???? ??? ?? ????. URL ?(?, ? ??? ??)? ? ???? ?? ??? ??? ? ????. ?? ? ???? ??? ???? ???? ?? ?? ?????? ?? ?? ????. ???? ??? ???????.
????? ?? ?? ??
http ?? ??: POST(https ????? ?????.)
api.weixin.qq.com/cgi-bi..._token=ACCESS_TOKEN ?? ??
{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "type":"click", "name":"歌手簡介", "key":"V1001_TODAY_SINGER" }, { "name":"菜單", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"view", "name":"視頻", "url":"http://v.qq.com/" }, { "type":"click", "name":"贊一下我們", "key":"V1001_GOOD" }] }] }
???? ??
?? ??
???? ???? JSON ??? ??? ??? ????. :
{"errcode":0,"errmsg":"ok"}?? ?? ? ???? JSON ??? ??? ??? ????(?: ??? ?? ?? ??):
{"errcode":40018,"errmsg":"??? ?? ?? ??"}6. ??
菜單的JSON結(jié)構(gòu)為
{"button": [{"name":"天氣預(yù)報","sub_button":[{"type":"click","name":"北京天氣","key":"天氣北 京"}, {"type":"click","name":"上海天氣","key":"天氣上海"}, {"type":"click","name":" 廣州天氣","key":"天氣廣州"},{"type":"click","name":"深圳天氣","key":"天氣深圳"}, {"type":"view","name":"本地天氣","url":"http://m.hao123.com/a/tianqi"}]}, {"name":"方倍工作室","sub_button":[{"type":"click","name":"公司簡 介","key":"company"}, {"type":"click","name":"趣味游戲","key":"游戲"}, {"type":"click","name":"講個笑話","key":"笑話"}]}]}
將以下代碼保存為menu.php,并且在瀏覽器中運行該文件(比如 127.0.0.1/menu.php),將直接向微信服務(wù)器提交菜單
php $access_token = ""; $jsonmenu = '{ "button":[ { "name":"天氣預(yù)報", "sub_button":[ { "type":"click", "name":"北京天氣", "key":"天氣北京" }, { "type":"click", "name":"上海天氣", "key":"天氣上海" }, { "type":"click", "name":"廣州天氣", "key":"天氣廣州" }, { "type":"click", "name":"深圳天氣", "key":"天氣深圳" }, { "type":"view", "name":"本地天氣", "url":"http://m.hao123.com/a/tianqi" }] }, { "name":"瑞雪", "sub_button":[ { "type":"click", "name":"公司簡介", "key":"company" }, { "type":"click", "name":"趣味游戲", "key":"游戲" }, { "type":"click", "name":"講個笑話", "key":"笑話" }] }] }'; $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; $result = https_request($url, $jsonmenu); var_dump($result); function https_request($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } ?>
或者使用官方的調(diào)試接口 使用網(wǎng)頁調(diào)試工具調(diào)試該接口
提交成功后,重新關(guān)注后即可看到菜單。菜單效果類似如下:
七、響應(yīng)菜單點擊事件
在消息接口中處理event事件,其中的click代表菜單點擊,通過響應(yīng)菜單結(jié)構(gòu)中的key值回應(yīng)消息,view事件無須響應(yīng),將直接跳轉(zhuǎn)過去
define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "text": $resultStr = $this->receiveText($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; default: $resultStr = ""; break; } echo $resultStr; }else { echo ""; exit; } } private function receiveText($object) { $funcFlag = 0; $contentStr = "你發(fā)送的內(nèi)容為:".$object->Content; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "歡迎洋洋博客"; case "unsubscribe": break; case "CLICK": switch ($object->EventKey) { case "company": $contentStr[] = array("Title" =>"公司簡介", "Description" =>"洋洋的博客", "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/pondbaystudio"); break; default: $contentStr[] = array("Title" =>"默認(rèn)菜單回復(fù)", "Description" =>"您正在使用的是<span style="font-family: Arial, Helvetica, sans-serif;">洋洋的博客</span><span style="font-family: Arial, Helvetica, sans-serif;">", </span> "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/pondbaystudio"); break; } break; default: break; } if (is_array($contentStr)){ $resultStr = $this->transmitNews($object, $contentStr); }else{ $resultStr = $this->transmitText($object, $contentStr); } return $resultStr; } private function transmitText($object, $content, $funcFlag = 0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag); return $resultStr; } private function transmitNews($object, $arr_item, $funcFlag = 0) { //首條標(biāo)題28字,其他標(biāo)題39字 if(!is_array($arr_item)) return; $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> <FuncFlag>%s</FuncFlag> </xml>"; $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $funcFlag); return $resultStr; } } ?>
八、菜單中獲取OpenID
由于菜單中只能填寫固定的url地址,對于想要菜單中獲取用戶的OpenID的情況,可以使用OAuth2.0授權(quán)的方式來實現(xiàn)。
URL中填寫的地址為一個固定的回調(diào)地址。原理方法可以參考 微信公眾平臺開發(fā)(99) 自定義菜單獲取OpenID
<?php /* 洋洋的博客 */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (isset($_GET['echostr'])) { $wechatObj->valid(); }else{ $wechatObj->responseMsg(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ header('content-type:text'); echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if($keyword == "?" || $keyword == "?") { $msgType = "text"; $contentStr = '當(dāng)前時間是:'.date("Y-m-d H:i:s",time()); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } }else{ echo ""; exit; } } } ?>
【相關(guān)推薦】
1. 特別推薦:“php程序員工具箱”V0.1版本下載
2. 微信公眾號平臺源碼下載
? ??? WeChat ?? ? ??? ?? ?? ?? ????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PhpisstillRelevantinmodernenterpriseenvironments.1. Modernphp (7.xand8.x)? ??? ??, ??? ??, jitcompilation ? modernsyntax, mateitsuilableforlarge-scalepplications

??? PHP ???? ???? ????? RabbitMQ? ???? ??? ??? ???? 1. ????? ??? ??? ?? ??? ???? ?? ???? ???????. 2. ???? ???? ?? ???? ???, ???? ???, ?? ? ?? ACK? ?????. 3. ?? ? ?? ? ??, TTL ? ?? ?? ? ?? ?? ??? ??????. 4. Supervisord? ?? ??? ???? ??? ????? ???? ??? ??? ???? ?? ?? ?? ????? ???????. ??? ????? ???? ??? ????? ???? ??? ?????.

N 1 ?? ??? ??? ?? ???? ?????? ?????? ?? ?? ????. 2. ???? ???? ???? ?? ??? ???????? ??? ??? ?? ? ??????. 3. ??? 2 ? ?? ?? Redis ?? ??? ?? ??? ?? ?? ??? ????? ??????. 4. ??? ????? ????? ????? Clear ()? ???? ???? ?????? ??? ????? ?????. 5. ?????? ??? ????? ???? ???? ? ??? ??? ?? ?? ? SQL ?? ??????. 6. ?? ??? ???? ?? ?????? ?? ?? ??? ?????? ?? ?? ?? ??? ???? ??? ??????. ORM? ???? ????? ?? ??? ????? ?? ???? ??? ???? ?? SQL ????, ??, ?? ?? ? ??? ???? ???????.

??? PHP ?? ???? ???? ???? ?? ??? ? Docker ??? ???? ?? ?? ????? ?????. 1. PHP : 8.3-fpm-alpine? ?? ???? ???? ?? ??? ??? ??? ??????. 2. ??? ?? php.ini? ?? ??? ??? ??????, ?? ??? ??, Opcache ? JIT? ????? ?? ? ??? ??????. 3. Nginx? ??? ???? ???? ??? ??? ?? ???? ???? PHP ??? PHP-FPM?? ???? ?????. 4. ??? ??? ???? ???? ?? ???? ???? ????? ????? ??? ?? ???? ??????. 5. CRON? ?? ?? ????? ?????? ??? ?? ??; 6. ?? ?? ??? ?? ??? ??? ??????

Subprocess.run ()? ???? ? ??? ???? ???? ??? ??????. ?? ??? ??? ?? ??? ?? ??? ???? ?? ????. 2. ? ??? ??? ??, shell = true? ??? ? ??? ?? ??? ??????. 3. ?? ????? ???? ??? ?? ??? ??????. 4. SET Check = ??? ?? ? ? ??? ??? ?? true; 5. ??? ?????? ??? ?? ???? ??? ?? ? ????. OS.System () ?? ? ?? ???? ?? ??? ???? ???? ?? ???? Subprocess.run ()? ?? ??? ???????. ?? ??? ????? ? ??? ???? ?? ??? ?????.

settings.json ??? ??? ?? ?? ?? ?? ?? ??? ??? VSCODE ??? ??? ???? ? ?????. 1. ??? ?? ?? : Windows? C : \ Users \\ AppData \ Roaming \ Code \ User \ Settings.json, MacOS IS /users//library/applicationsupport/code/user/settings.json, linux? /home//.config/code/user/settings.json; 2. Workspace ?? ?? : .vscode/settings project root ????

PHP? ??? ?? ????? ?? ??? ??????????? ?? ??? ???? ?? ?? ??? ????????. 1. ?? ??? ??? ?? ??? ?? ? ?? ???? ?????. 2. ?? ???? ???? ???? ?? ? ? ???? GC? ?? ????? ???? ?????. 3. "??? ??"zval? ?? ?? ????? ???? GC_COLLECT_CYCLES ()? ?? ? ? GC? ??????. 4. ?? ?? PHP ?? ????? ??? ??? ??? ?? GC_STATUS ()? ?????? GC_COLLECT_CYCLES ()? ? ??? ???????. 5. ?? ???? ?? ??? ??? GC_DISABLE ()? ???? ?? ? ??? ????? ORM? CLER () ???? ?? DeReeference ??? ?????.

BREF? ?? PHP ???? ??? ???? ?? ?? ???? ?? ???? ?? ????? ?? ? ? ????. 1. Bref? ??? ? PHP ??? ???? ???? PHP8.3 ? ?? ??? ???? Laravel ? Symfony? ?? ??? ??? ???? ???? PHP? Awslambda? ?????. 2. ?? ???? ??? ????? : Composer? ???? BREF ??, HTTP ?? ??? ? ?? ??? ?? ?? ? ???? ???? ?? Serverless.yml ??; 3. ServerlessDeploy ??? ???? ??? ???? APIGINGWARE? ???? ???? ??? URL? ?????. 4. Lambda ??? ?? Bref? ???? ?????.
