无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了PHP封装的微信公众平台接口开发操作类。分享给大家供大家参考,具体如下:
示例调用 index.php
<"mpweixin.class.php"; $this->class_obj = new mpweixin($this->config); //实例化对应的插件 } } /** * 服务器地址(用户消息和开发者需要的事件推送,会被转发到该URL中) */ public function index() { if ($this->is_check_signature) { $this->valid(); }else{ $this->class_obj->responseMsg(); } } /** * 开发者对签名(即signature)的效验,来判断此条消息的真实性 */ public function valid() { $echoStr = $this->checkSignature(); if($echoStr){ header('content-type:text'); exit($echoStr); } } /** * 验证是否来自于微信 * @return [type] [description] */ public function checkSignature() { //微信会发送4个参数到我们的服务器后台 签名 时间戳 随机字符串 随机数 $signature = $_GET['signature']; // 签名 $timestamp = $_GET['timestamp']; // 时间戳 $echoStr = $_GET['echostr']; // 随机字符串 $nonce = $_GET['nonce']; // 随机数 if ($signature && $timestamp && $echoStr && $nonce) { // 微信公众号基本配置中的token $token = TOKEN; //将token、timestamp、nonce按字典序排序 $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); // 将三个参数字符串拼接成一个字符串进行sha1加密 $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); // 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 if($tmpStr == $signature){ return $echoStr; } else { return false; } } } } "htmlcode"><"php://input"); if (!empty($postStr)){ $this->logger("R \r\n".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { // 事件 case "event": $result = $this->receiveEvent($postObj); break; // 文本 case "text": if (strstr($postObj->Content, "第三方")){ $result = $this->relayPart3("http://www.fangbei.org/test.php".'"image": $result = $this->receiveImage($postObj); break; // 地理位置 case "location": $result = $this->receiveLocation($postObj); break; // 语音 case "voice": $result = $this->receiveVoice($postObj); break; // 视频 case "video": $result = $this->receiveVideo($postObj); break; // 链接 case "link": $result = $this->receiveLink($postObj); break; default: $result = "unknown msg type: ".$RX_TYPE; break; } $this->logger("T \r\n".$result); echo $result; }else { echo ""; exit; } } /* ----------------------------------接收----------------------------- */ /** * 接收事件消息 */ private function receiveEvent($object) { $content = ""; switch ($object->Event) { // 关注时的事件推送 case "subscribe": $content = "欢迎关注方倍工作室"; $content .= (!empty($object->EventKey)) "\n来自二维码场景".str_replace("qrscene_","",$object->EventKey)) : ""; break; // 取消关注事件 case "unsubscribe": $content = "取消关注"; break; // 点击菜单拉取消息时的事件推送 case "CLICK": switch ($object->EventKey) { case "COMPANY": $content = array(); $content[] = array("Title"=>"方倍工作室", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg";, "Url" =>"http://m.cnblogs.com/";); break; default: $content = "点击菜单:".$object->EventKey; break; } break; // 点击菜单跳转链接时的事件推送 case "VIEW": $content = "跳转链接 ".$object->EventKey; break; // 扫描带参数二维码场景,用户已关注时的事件推送 case "SCAN": $content = "扫描场景 ".$object->EventKey; break; // 上报地理位置事件 case "LOCATION": $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude; break; // 扫码推事件且弹出“消息接收中”提示框的事件推送 case "scancode_waitmsg": if ($object->ScanCodeInfo->ScanType == "qrcode"){ $content = "扫码带提示:类型 二维码 结果:".$object->ScanCodeInfo->ScanResult; }else if ($object->ScanCodeInfo->ScanType == "barcode"){ $codeinfo = explode(",",strval($object->ScanCodeInfo->ScanResult)); $codeValue = $codeinfo[1]; $content = "扫码带提示:类型 条形码 结果:".$codeValue; }else{ $content = "扫码带提示:类型 ".$object->ScanCodeInfo->ScanType." 结果:".$object->ScanCodeInfo->ScanResult; } break; // 扫码推事件的事件推送 case "scancode_push": $content = "扫码推事件"; break; // 弹出系统拍照发图的事件推送 case "pic_sysphoto": $content = "系统拍照"; break; // 弹出微信相册发图器的事件推送 case "pic_weixin": $content = "相册发图:数量 ".$object->SendPicsInfo->Count; break; // 弹出拍照或者相册发图的事件推送 case "pic_photo_or_album": $content = "拍照或者相册:数量 ".$object->SendPicsInfo->Count; break; // 弹出地理位置选择器的事件推送 case "location_select": $content = "发送位置:标签 ".$object->SendLocationInfo->Label; break; default: $content = "receive a new event: ".$object->Event; break; } if(is_array($content)){ if (isset($content[0]['PicUrl'])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } /** * 接收文本消息 */ private function receiveText($object) { $keyword = trim($object->Content); //多客服人工回复模式 if (strstr($keyword, "请问在吗") || strstr($keyword, "在线客服")){ $result = $this->transmitService($object); return $result; } //自动回复模式 if (strstr($keyword, "文本")){ $content = "这是个文本消息"; }else if (strstr($keyword, "表情")){ $content = "中国:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335); }else if (strstr($keyword, "单图文")){ $content = array(); $content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg";, "Url" =>"http://m.cnblogs.com/";); }else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){ $content = array(); $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg";, "Url" =>"http://m.cnblogs.com/";); $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg";, "Url" =>"http://m.cnblogs.com/";); $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg";, "Url" =>"http://m.cnblogs.com/";); }else if (strstr($keyword, "音乐")){ $content = array(); $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3";, "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3";); }else{ $content = date("Y-m-d H:i:s",time())."\nOpenID:".$object->FromUserName."\n技术支持 方倍工作室"; } if(is_array($content)){ if (isset($content[0])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } /** * 接收图片消息 */ private function receiveImage($object) { $content = array("MediaId"=>$object->MediaId); $result = $this->transmitImage($object, $content); return $result; } /** * 接收位置消息 */ private function receiveLocation($object) { $content = "你发送的是位置,经度为:".$object->Location_Y.";纬度为:".$object->Location_X.";缩放级别为:".$object->Scale.";位置为:".$object->Label; $result = $this->transmitText($object, $content); return $result; } /** * 接收语音消息 */ private function receiveVoice($object) { if (isset($object->Recognition) && !empty($object->Recognition)){ $content = "你刚才说的是:".$object->Recognition; $result = $this->transmitText($object, $content); }else{ $content = array("MediaId"=>$object->MediaId); $result = $this->transmitVoice($object, $content); } return $result; } /** * 接收视频消息 */ private function receiveVideo($object) { $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>""); $result = $this->transmitVideo($object, $content); return $result; } /** * 接收链接消息 */ private function receiveLink($object) { $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url; $result = $this->transmitText($object, $content); return $result; } /* ----------------------------------回复----------------------------- */ /** * 回复文本消息 */ private function transmitText($object, $content) { if (!isset($content) || empty($content)){ return ""; } $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } /** * 回复多客服消息 */ private function transmitService($object) { $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /** * 回复图文消息 */ private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return ""; } $itemTpl = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles>$item_str</Articles> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } /** * 回复音乐消息 */ private function transmitMusic($object, $musicArray) { if(!is_array($musicArray)){ return ""; } $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /** * 回复图片消息 */ private function transmitImage($object, $imageArray) { $itemTpl = "<Image> <MediaId><![CDATA[%s]]></MediaId> </Image>"; $item_str = sprintf($itemTpl, $imageArray['MediaId']); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /** * 回复语音消息 */ private function transmitVoice($object, $voiceArray) { $itemTpl = "<Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice>"; $item_str = sprintf($itemTpl, $voiceArray['MediaId']); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /** * 回复视频消息 */ private function transmitVideo($object, $videoArray) { $itemTpl = "<Video> <MediaId><![CDATA[%s]]></MediaId> <ThumbMediaId><![CDATA[%s]]></ThumbMediaId> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> </Video>"; $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[video]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /* ----------------------------------通用----------------------------- */ /** * 回复第三方接口消息 */ private function relayPart3($url, $rawData) { $headers = array("Content-Type: text/xml; charset=utf-8"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData); $output = curl_exec($ch); curl_close($ch); return $output; } /** * 字节转Emoji表情 */ function bytes_to_emoji($cp) { if ($cp > 0x10000){ # 4 bytes return chr(0xF0 | (($cp & 0x1C0000) 18)).chr(0x80 | (($cp & 0x3F000) 12)).chr(0x80 | (($cp & 0xFC0) 6)).chr(0x80 | ($cp & 0x3F)); }else if ($cp > 0x800){ # 3 bytes return chr(0xE0 | (($cp & 0xF000) 12)).chr(0x80 | (($cp & 0xFC0) 6)).chr(0x80 | ($cp & 0x3F)); }else if ($cp > 0x80){ # 2 bytes return chr(0xC0 | (($cp & 0x7C0) 6)).chr(0x80 | ($cp & 0x3F)); }else{ # 1 byte return chr($cp); } } /** * 日志记录 */ private function logger($log_content) { if (isset($_SERVER['HTTP_APPNAME'])) { //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); } else if ($_SERVER['REMOTE_ADDR'] != "127.0.0.1") { //LOCAL $max_size = 1000000; $log_filename = "mpweixin.log.xml"; if (file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)) { unlink($log_filename); } file_put_contents($log_filename, date('Y-m-d H:i:s')." ".$log_content."\r\n", FILE_APPEND); } } /** * 获取access_token */ private function getAccessToken() { $token_url = "https://api.weixin.qq.com/cgi-bin/token"; . $this->appid . "&secret=" . $this->secret; $response = $this->get_contents($token_url); $params = array(); $params = json_decode($response,true); if (isset($params['errcode'])) { echo "<h3>error:</h3>" . $params['errcode']; echo "<h3>msg :</h3>" . $params['errmsg']; exit; } return $params['access_token']; } public function get_contents($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_URL, $url); $response = curl_exec($ch); curl_close($ch); //-------请求为空 if(empty($response)){ exit("50001"); } return $response; } } "_blank" href="//www.jb51.net/Special/556.htm">PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《PHP中json格式数据操作技巧汇总》及《PHP针对XML文件操作技巧总结》希望本文所述对大家PHP程序设计有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月17日
2024年11月17日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】