業務邏輯文件編寫
use think\Action; //自己封裝的curl方法,詳情看附錄 define("TOKEN", "你設置的token"); class Customer extends Controller { //校驗服務器地址URL public function checkServer(){ if (isset($_GET['echostr'])) { $this->valid(); }else{ $this->responseMsg(); } } public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ header('content-type:text'); echo $echoStr; exit; }else{ echo $echoStr.'+++'.TOKEN; 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() { //此處推薦使用file_get_contents('php://input')獲取后台post過來的數據 $postStr = file_get_contents('php://input'); if (!empty($postStr) && is_string($postStr)){ $postArr = json_decode($postStr,true); if(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'text'){ //用戶給客服發送文本消息 if($postArr['Content'] == 7){ //接收到指定的文本消息,觸發事件 $fromUsername = $postArr['FromUserName']; //發送者openid $media_id = '上傳到微信服務器的圖片id,看第三部分'; //輸入想要回復的圖片消息的media_id $this->requestIMAGE($fromUsername,$media_id); } } else if(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'image'){ //用戶給客服發送圖片消息,按需求設置 } else if($postArr['MsgType'] == 'event' && $postArr['Event']=='user_enter_tempsession'){ //用戶進入客服事件 $fromUsername = $postArr['FromUserName']; //發送者openid $content = '你好,歡迎來到***客服,有什么能幫助您的么'; $this->requestTXT($fromUsername,$content); } else{ exit('error'); } }else{ echo "empty"; exit; } } //文本回復 public function requestTXT($fromUsername,$content){ $data=array( "touser"=>$fromUsername, "msgtype"=>"text", "text"=>array("content"=>$content) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); $this->requestAPI($json); } //圖片回復 public function requestIMAGE($fromUsername,$media_id){ $data=array( "touser"=>$fromUsername, "msgtype"=>"image", "image"=>array("media_id"=>$media_id) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); $this->requestAPI($json); } public function requestAPI($json){ $access_token = $this->get_accessToken(); $action = new Action(); //自己封裝的curl方法,詳情看附錄 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token; $output = $action->curl_post($url,$json); if($output == 0){ echo 'success'; exit; } } //調用微信api,獲取access_token,有效期7200s public function get_accessToken(){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的秘鑰'; //替換成自己的小程序id和secret $res = file_get_contents($url); $data = json_decode($res,true); $token = $data['access_token']; return $token; } }
上傳回復圖片到微信服務器
這個是臨時的素材接口,只能存在3天,目前小程序不支持永久素材上傳,只有公眾號支持
public function uploadWxMedia(){ $token = $this->get_accessToken(); $type = "image"; $filepath = Env::get('root_path').'public\\assets\\imageName.png'; //文件在服務器的絕對路徑,按自己存放位置修改 $data = array("media"=>new \CURLFile($filepath)); //php5.6以上必須用這種方法上傳文件 $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=".$type; $action = new Action(); //封裝的curl方法,看附錄 $result = $action->curl_post($url,$data); print_r($result); } //調用微信api,獲取access_token,有效期7200s public function get_accessToken(){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的秘鑰'; //替換成自己的小程序id和secret $res = file_get_contents($url); $data = json_decode($res,true); $token = $data['access_token']; return $token; }
訪問 uploadWxMedia() 方法就會把設置好的圖片上傳,會返回一個json數據:
{"type":"image","media_id":"LTbNsi***************JqG","created_at":1558062553}
其中的 media_id 就是用來填寫進第二步的回復圖片中的值
附錄
封裝的curl方法
namespace think; class Action { //get方式請求接口 public function get_json($url) { $data = file_get_contents($url); //轉換成數組 $data = json_decode($data,true); //輸出 return $data; } //post方式請求接口 public function curl_post($url,$data,$headers = null) { //$data 是一個 array() 數組;未編碼 $curl = curl_init(); // 啟動一個CURL會話 if(substr($url,0,5)=='https'){ // 跳過證書檢查 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //只有在CURL低於7.28.1時CURLOPT_SSL_VERIFYHOST才支持使用1表示true,高於這個版本就需要使用2表示了(true也不行)。 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); if($headers != null){ //post請求中攜帶header參數 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } //返回api的json對象 $response = curl_exec($curl); //關閉URL請求 curl_close($curl); //返回json對象 return $response; } }