基于PHP實現(xiàn)微信小程序客服消息功能
項目說明:
本項目是一個簡單微信小程序客服消息類,實現(xiàn)客服消息相關(guān)功能。官方給的php示例有誤,這里就不再吐槽了。
本示例是采用開發(fā)者服務(wù)器,沒有采用云調(diào)用的形式。
官方文檔:
適用場景
客戶消息流程圖
使用步驟
1、開啟客服消息
https://mp.weixin.qq.com/wxam...
登錄-開發(fā)-開發(fā)設(shè)置-消息推送
[]( https://raw.githubusercontent...
點擊“啟動”
[]( https://raw.githubusercontent...
URL(服務(wù)器地址):填開發(fā)者服務(wù)器對應(yīng)的url,如 https://xxxxxx/demo.php
Token(令牌):這個隨便填,要求3-32位。
EncodingAESKey(消息加密密鑰):這個點擊“隨機生成”即可。
消息加密方式:可以根據(jù)自己需要選擇,本例選擇”兼容模式“。
數(shù)據(jù)格式:json相對于xml來說,從壓縮效率及傳輸效率更具優(yōu)勢,這里我們選json。
注意:以上操作完后先不要提交,等配置好開發(fā)者服務(wù)端后再提交。
2、配置開發(fā)者服務(wù)端
檢驗signature的PHP示例代碼:
$signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $echostr=$_GET["echostr"]; $token = TOKEN;//這里改成你第一步操作時填寫的token $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if ($tmpStr == $signature ) { return $echostr; } else { return false; }
官方示例沒有返回 $echostr
,這個檢驗開發(fā)者服務(wù)端是否成功的關(guān)鍵,必須返回。
3、提交消息推送配置
如果沒有報錯,證明配置成功。
4、開發(fā)者服務(wù)端demo
<?php //驗證signature //$signature = $_GET["signature"]; //$timestamp = $_GET["timestamp"]; //$nonce = $_GET["nonce"]; //$echostr=$_GET["echostr"]; // //$token = TOKEN;//這里改成你第一步操作時填寫的token //$tmpArr = array($token, $timestamp, $nonce); //sort($tmpArr, SORT_STRING); //$tmpStr = implode( $tmpArr ); //$tmpStr = sha1( $tmpStr ); // //if ($tmpStr == $signature ) { // return $echostr; //} else { // return false; //} include_once './Xcxmsg.php'; $xcxmsg = new Xcxmsg(); $postStr = file_get_contents('php://input'); if (!$postStr) return false; $postArr = json_decode($postStr, true); if (!isset($postArr['MsgType']) || !isset($postArr['FromUserName'])) return false; $data = ["touser" => $postArr['FromUserName']]; $accessToken = $xcxmsg->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken; switch ($postArr['MsgType']) { case "text": //如用戶發(fā)送的是文字信息,這里處理 //回復(fù)圖文鏈接,也可以回復(fù)別的類型,根據(jù)需要 $data['msgtype'] = "link"; $data['link'] = [ "title" => "hello", "description" => "Is Really A Happy Day", "url" => "LINK_URL",//連接url "thumb_url" =>"THUMB_URL" //圖片url ]; $json = json_encode($data, JSON_UNESCAPED_UNICODE); $xcxmsg->curl($json, $url); break; case "image": //如用戶發(fā)送圖片消息,進(jìn)入這里 //服務(wù)端回復(fù) 圖片,也可以回復(fù)別的類型,根據(jù)需要 $data['msgtype'] = "image"; $data['image'] = ['media_id' => 'media_id值']; // 執(zhí)行 $xcxmsg->upload($accessToken)返回的 media_id $json = json_encode($data, JSON_UNESCAPED_UNICODE); $xcxmsg->curl($json, $url); case "miniprogrampage": //如用戶發(fā)送小程序卡片,進(jìn)入這里 //這里服務(wù)端回復(fù)小卡片,也可以回復(fù)別的類型,根據(jù)需要 $data['msgtype'] = "miniprogrampage"; $data['miniprogrampage'] = [ "title" => "title", "pagepath" => "pages/index/index", "thumb_media_id" => "media_id值"];// 執(zhí)行 $xcxmsg->upload($accessToken)返回的 media_id $json = json_encode($data, JSON_UNESCAPED_UNICODE); $xcxmsg->curl($json, $url); break; case "event": //如用戶進(jìn)入會話事件 //這里可以回復(fù)文本 $data['msgtype'] = "text"; $data['text'] = [ "content" => "Hello World", ]; $json = json_encode($data, JSON_UNESCAPED_UNICODE); $xcxmsg->curl($json, $url); break; default: }
5、小程序前端
在需要的地方添加以下代碼:
<button open-type="contact" >客服消息</button>
用微信開發(fā)工具的預(yù)覽,生成二維碼,掃描測試是否成功。
項目地址: https://github.com/guyan0319/...
總結(jié)
以上所述是小編給大家介紹的基于PHP實現(xiàn)微信小程序客服消息功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
詳解PHP中strlen和mb_strlen函數(shù)的區(qū)別
在PHP中,strlen與mb_strlen是求字符串長度的函數(shù),但是對于一些初學(xué)者來說,如果不看手冊,也許不太清楚其中的區(qū)別2014-03-03PHP數(shù)據(jù)庫鏈接類(PDO+Access)實例分享
這篇文章主要介紹了PHP數(shù)據(jù)庫鏈接類(PDO+Access),有需要的朋友可以參考一下2013-12-12Laravel框架實現(xiàn)的使用smtp發(fā)送郵件功能示例
這篇文章主要介紹了Laravel框架實現(xiàn)的使用smtp發(fā)送郵件功能,結(jié)合實例形式分析了Laravel框架相關(guān)配置及郵件發(fā)送操作技巧,需要的朋友可以參考下2019-03-03TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作示例
這篇文章主要介紹了TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作,結(jié)合實例形式分析了MongoDB擴(kuò)展的基本安裝、配置、模型操作以及使用Push操作實現(xiàn)的數(shù)據(jù)添加、更新等方法,需要的朋友可以參考下2018-09-09