夺命雷公狗---微信开发26----客服消息接口基础和推送视频
生活随笔
收集整理的這篇文章主要介紹了
夺命雷公狗---微信开发26----客服消息接口基础和推送视频
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們按照上一課的套路繼續改寫下index.php即可實現,代碼如下所示:
?
<?php /*** wechat php test*///define your token require_once "common.php"; //這里是引入curl發送函數的類 require_once 'WeChat.class.php'; define("TOKEN", "twgdh");//這里讓這個類繼承了curl發送參數的類 class wechatCallbackapiTest extends WeChat {public function valid(){$echoStr = $_GET["echostr"];//valid signature , optionif($this->checkSignature()){echo $echoStr;exit;}}public function responseMsg(){//get post data, May be due to the different environments$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//extract post dataif (!empty($postStr)){/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,the best way is to check the validity of xml by yourself */// 使用simplexml技術對xml進行解析 // libxml_disable_entity_loader(true), 是從安全性考慮,為了防止xml外部注入,//只對xml內部實體內容進行解析libxml_disable_entity_loader(true);//加載 postStr 字符串$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);file_put_contents('abc.log', "\r\n\r\n". $postStr, FILE_APPEND);$fromUsername = $postObj->FromUserName;file_put_contents('abc.log', "\r\n\r\n". $fromUsername, FILE_APPEND);$toUsername = $postObj->ToUserName;file_put_contents('abc.log', "\r\n\r\n". $toUsername, FILE_APPEND);$keyword = trim($postObj->Content);$time = time();global $tmp_arr;//根據接收到的消息類型,來進行分支處理(switch)switch($postObj->MsgType){case 'event':if($postObj->Event == 'subscribe'){$contentStr = "歡迎關注leigood微信測試號噢";$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);echo $resultStr; }//響應用戶的點擊事件if($postObj->Event == 'CLICK'){if($postObj->EventKey == 'V1001_TODAY_MUSIC'){ //自定義菜單里面有很多個,這里key是自定義的//這里主要寫自己想要的業務邏輯$contentStr = "奪命雷公狗歡迎您來到編程世界";$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);echo $resultStr; }}break;case 'text': //回復文本模塊//這里開始測試客服消息接口了if($keyword == '文本'){//獲取tokenrequire 'get_token.php';$contentStr = '這是客服接口的回復,我們可以幫助您什么?';//對發送的內容進行urlencode編碼,防止中文亂碼$contentStr = urlencode($contentStr);//到時候我們我發送的內容我們放到一個數組里面去了$content_arr = array('content'=>"{$contentStr}");//這里的意思是將來我要發送消息給這個用戶$reply_arr = array('touser'=>"{$fromUsername}",'msgtype'=>'text','text'=>$content_arr);//下一步就是將編碼轉成規定的json格式$post = json_encode($reply_arr);//url解碼,如果不解碼他將會發來一段二進制代碼$post = urldecode($post);$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";//這里我們來使用類的繼承機制,來簡化代碼$this -> http_request($url,$post);//這里是可以回復多條消息的,只需要在執行下這個函數即可//$this -> http_request($url,$post); }else if($keyword == '圖片'){//這里的media_id以后是從數據庫里面進行查詢出來的$media_id = 'e9-vVDge44mS0gRN2SbE_oOtY64JVozQBSdkV4SIJnPnJjgRDZTBxrS-YGbd72Z3';$content_arr = array('media_id'=>"{$media_id}");$reply_arr = array('touser'=>"{$fromUsername}",'msgtype'=>'image','image'=>$content_arr);$post = json_encode($reply_arr);//引入token值$access_token = $this -> get_token();$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";//CURL發送$this -> http_request($url,$post);}else if($keyword == '歌曲'){//這里的media_id以后是從數據庫里面進行查詢出來的$media_id = 'y-a1TVDFOrdarZSLI-BYXLWaa-KvKYxO4errfsrPrF-CgNHSUm0tmICsAw1oFkcz';$content_arr = array('media_id'=>"{$media_id}");$reply_arr = array('touser'=>"{$fromUsername}",'msgtype'=>'voice','voice'=>$content_arr);//讓他進行json格式編碼$post = json_encode($reply_arr);//引入token值$access_token = $this -> get_token();$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";//CURL發送$this -> http_request($url,$post);//讓他推送多一段語音$this -> http_request($url,$post);}else if($keyword == '視頻'){//這里的media_id以后是從數據庫里面進行查詢出來的$media_id = '5tkUzLBkiD_wDwEVS05CWp7yXQCHKcHKIWcZUV7Q8pnvwN-WSPBJKRNzgLkaBwLG';$title = '人狗請未了';$title = urlencode($title); //必須處理,否則將來是亂碼$description = '描述人狗請未了之熟女也喜歡';$description = urlencode($description);//必須處理,否則將來是亂碼$content_arr = array('media_id'=>"{$media_id}",'title'=>"{$title}",'description'=>"{$description}");//手冊上還有一個thumb_media_id(縮略圖)這個可以不填寫,沒有大不了就是不顯示$reply_arr = array('touser'=>"{$fromUsername}",'msgtype'=>'video','video'=>$content_arr);//讓他進行json格式編碼$post = json_encode($reply_arr);//url解碼$post = urldecode($post);//引入token值$access_token = $this -> get_token();$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";//CURL發送$this -> http_request($url,$post);//讓他推送多兩段視頻$this -> http_request($url,$post);$this -> http_request($url,$post);}else{$contentStr = '您輸入的格式有誤';$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);echo $resultStr;}break;case 'image': //處理用戶上傳圖片$media_id = $postObj -> MediaId; //獲取到用戶上傳的圖片的mediaid$resultStr = sprintf($tmp_arr['image'], $fromUsername, $toUsername, $time, $media_id);echo $resultStr;//將圖片保存到本地服務器的文件系統//1.先給圖片創建一個名字$image_file_name = time().'.jpg';//2.獲取該圖片的內容$image_file = file_get_contents($postObj->PicUrl);//3.保存到本地服務器的文件系統//提醒:一定要保證您創建的文件夾是www用戶可讀可寫,否則無法保存該圖片到文件夾下file_put_contents("./uploadimage/".$image_file_name,$image_file);//將圖片的路徑和相關信息入庫//1.創建一張表//2.鏈接mysql數據庫,并且添加圖片信息$connect = mysql_connect('localhost','root','root');mysql_select_db('wxdb',$connect);mysql_query('set names utf8');$media_path = "./uploadimage/".$image_file_name; //路徑$sql = "insert into keep_image_uploads (id,openid,media_id,media_path) values(NULL,'{$fromUsername}','{$media_id}','{$media_path}')";mysql_query($sql,$connect);break;case 'voice': //處理用戶上傳語言的業務邏輯$media_id = $postObj -> MediaId; //獲取media_id的id號$resultStr = sprintf($tmp_arr['voice'], $fromUsername, $toUsername, $time, $media_id);echo $resultStr;break;case 'location': //處理用戶上傳的地理位置信息$Location_X = $postObj -> Location_X; //獲取上傳地理位置的緯度$Location_Y = $postObj -> Location_Y; //獲取上傳地地理位置經度$contentStr = "您上報的地理位置是:\n經度是:{$Location_Y} \n緯度是: {$Location_X}";$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);echo $resultStr;break;case 'link': //接收并回復鏈接信息//獲取到用戶上傳的鏈接信息$Title = $postObj -> Title;$Url = $postObj -> Url;$contentStr = "<a href='{$Url}'>{$Title}</a>";$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);echo $resultStr;break;}}else {echo "";exit;}}private function checkSignature(){// you must define TOKEN by yourselfif (!defined("TOKEN")) {throw new Exception('TOKEN is not defined!');}$signature = $_GET["signature"];$timestamp = $_GET["timestamp"];$nonce = $_GET["nonce"];$token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);// use SORT_STRING rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}} }//如果這段代碼放在上面,那程序將會報錯,因為繼承的問題,會顯示類沒有找到 $wechatObj = new wechatCallbackapiTest(); //當接入成功后,請注銷這句話,否則,會反復驗證。 //$wechatObj->valid(); //添加響應請求的語句 $wechatObj->responseMsg();?>?
?
核心代碼如下:
轉載于:https://www.cnblogs.com/leigood/p/5202582.html
總結
以上是生活随笔為你收集整理的夺命雷公狗---微信开发26----客服消息接口基础和推送视频的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows下使用net-snmp实现
- 下一篇: eclipse下查看maven下载的源码