本文实例为大家分享了微信开发php代码,供大家参考,具体内容如下
<?php //封装成一个微信接口类 class weixinapi { private $appid; private $appsecret; //构造方法 初始化赋值 public function __construct($appid="",$appsecret="") { $this->appid = $appid; $this->appsecret = $appsecret; } //验证服务器地址有效性 public function valid() { if($this->checksignature()) { $echostr = $_get['echostr'];//随机的字符串 return $echostr; } else { return "error"; } } //检查签名 private function checksignature() { //一、接收微信服务器get方式提交过来的4个参数数据 $signature = $_get['signature'];//微信加密签名 $timestamp = $_get['timestamp'];//时间戳 $nonce = $_get['nonce'];//随机数 //二、加密/校验过程 // 1. 将token、timestamp、nonce三个参数进行字典序排序; // bool sort ( array &$array [, int $sort_flags = sort_regular ] ) 对数组排序 $tmparr = array(token,$timestamp,$nonce);//将上面三个参数放到一个数组里面 sort($tmparr,sort_string); // 2. 将三个参数字符串拼接成一个字符串进行sha1加密; $tmpstr = implode($tmparr); //将数组转化成字符串 $signaturestr = sha1($tmpstr); // 3. 开发者获得加密后的字符串与signature对比。 if($signaturestr == $signature) { return true; } else { return false; } } //响应消息 public function responsemsg() { //接收微信服务器发送post请求到开发者服务器,携带的xml数据包 $postdata = $globals['http_raw_post_data']; //处理xml数据包 $xmlobj = simplexml_load_string($postdata,"simplexmlelement",libxml_nocdata); if(!$xmlobj) { echo ""; exit; } //获取接收消息中的参数内容 $tousername = $xmlobj->tousername;//开发者微信号 $fromusername = $xmlobj->fromusername;//发送方的微信号(openid) $msgtype = $xmlobj->msgtype;//消息类型 switch ($msgtype) { //接收文本消息 case 'text': //获取文本消息的关键字 $keyword = $this->receivetext($xmlobj); //进行关键字回复 switch($keyword) { case "w001": case "w001": return $this->replytext($xmlobj,"hi~你好"); break; case "w002": case "w002": return $this->replytext($xmlobj,"hi~尴尬了"); break; case "笑话": $key = "dee9ebc68fd5a61f67286063932afe56"; return $this->replynews($xmlobj,$this->joke_text($key)); break; default: $key = "dee9ebc68fd5a61f67286063932afe56"; return $this->replynews($xmlobj,$this->joke_text($key)); break; } break; //接收图片消息 case 'image': return $this->receiveimage($xmlobj); break; //接收事件推送 case 'event': return $this->receiveevent($xmlobj); break; } } //接收事件推送 public function receiveevent($obj) { //接收事件类型 $event = $obj->event; switch ($event) { //关注事件 case 'subscribe': //下发欢迎消息 $newsarr = array( array( "title"=>"做有价值的头条资讯!", "description"=>"把握价值头条资讯,日常更加有谈资呢!", "picurl"=>"http://jober.applinzi.com/news/img/news.png", "url"=>"http://jober.applinzi.com/news/index.php" ) ); //回复图文消息 return $this->replynews($obj,$newsarr); break; //取消关注事件 case 'unsubscribe': //账号的解绑操作等等 break; //自定义菜单推送click事件 case 'click': $eventkey = $obj->eventkey;//获取事件key值,与自定义菜单接口中key值对应 switch ($eventkey) { case 'old': $weixinarr = $this->history("da675ebc6a0d72920dca3f676122a693"); $weixinarr = array_slice($weixinarr, 0,5); $newsarr = array(); foreach ($weixinarr as $item) { $newsarr = array(array( "title" => $item['description'], "description" => $item['title'], "picurl" => "http://1.jober.applinzi.com/news/img/2.jpg", "url" => "http://www.todayonhistory.com/" )); } return $this->replynews($obj,$newsarr); break; } break; } } //接收文本消息 public function receivetext($obj) { $content = trim($obj->content);//文本消息的内容 return $content; } //接收图片消息 public function receiveimage($obj) { $picurl = $obj->picurl;//图片的链接 $mediaid = $obj->mediaid;//图片消息媒体id return $this->replyimage($obj,$mediaid); } //回复图片消息 public function replyimage($obj,$mediaid) { $replyxml = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[image]]></msgtype> <image> <mediaid><![cdata[%s]]></mediaid> </image> </xml>"; return sprintf($replyxml,$obj->fromusername,$obj->tousername,time(),$mediaid); } //回复文本消息 public function replytext($obj,$content) { $replyxml = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[text]]></msgtype> <content><![cdata[%s]]></content> </xml>"; return sprintf($replyxml,$obj->fromusername,$obj->tousername,time(),$content); } //回复图文消息 public function replynews($obj,$newsarr) { //判断是否为数组类型 if(!is_array($newsarr)) { return; } // 判断数组是否为空数组 if(!$newsarr) { return; } $itemstr = ""; //定义item模板 $itemxml = "<item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item>"; foreach($newsarr as $item) { $itemstr .= sprintf($itemxml,$item['title'],$item['description'],$item['picurl'],$item['url']); } $replyxml = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[news]]></msgtype> <articlecount>".count($newsarr)."</articlecount> <articles>".$itemstr."</articles> </xml>"; return sprintf($replyxml,$obj->fromusername,$obj->tousername,time()); } //封装https请求(get和post) protected function https_request($url,$data=null) { //1、初始化curl $ch = curl_init(); //2、设置传输选项 curl_setopt($ch, curlopt_url, $url);//请求的url地址 curl_setopt($ch,curlopt_returntransfer,1);//将请求的结果以文件流的形式返回 if(!empty($data)) { curl_setopt($ch,curlopt_post,1);//请求post方式 curl_setopt($ch,curlopt_postfields,$data);//post提交的内容 } //3、执行请求并处理结果 $outopt = curl_exec($ch); //把json数据转化成数组 $outoptarr = json_decode($outopt,true); //4、关闭curl curl_close($ch); //如果返回的结果$outopt是json数据,则需要判断一下 if(is_array($outoptarr)) { return $outoptarr; } else { return $outopt; } } public function juhe_weixin($key,$type) { $url ="http://v.juhe.cn/toutiao/index?type={$type}&key={$key}"; $result = $this->https_request($url); if($result['error_code'] == 0) { return $result['result']['data']; } else { return array(); } } //聚合数据-获取最新趣图 public function joke_text($key,$pagesize=10) { $url = "http://japi.juhe.cn/joke/img/text.from?key={$key}&pagesize={$pagesize}"; $jokearr = $this->https_request($url); $resultarr = $jokearr['result']['data']; // $content = $resultarr[0]['content']; // return $this->replytext($xmlobj,$content); $newsarr = array(); //判断笑话接口是否获取数据 if($jokearr['error_code'] == 0) { foreach($resultarr as $item) { $newsarr[] = array( "title"=>$item['content'], "description"=>$item['updatetime'], "picurl"=>$item['url'], "url"=>$item['url'] ); } } return $newsarr; } //聚合数据-获取历史上的今天 public function history($key) { $m = idate('m'); $d = idate('d'); $day = "{$m}/{$d}"; $url = "http://v.juhe.cn/todayonhistory/queryevent.php?key={$key}&date={$day}"; $historyarr = $this->https_request($url); $resultarr = $historyarr['result']; // $content = $resultarr['title']; // return $this->replytext($xmlobj,$content); $newsarr = array(); //判断接口是否获取数据 if($jokearr['error_code'] == 0) { foreach($resultarr as $item) { $newsarr[] = array( "title"=>$item['title'], "description"=>$item['date'], "picurl"=>"", "url"=>"" ); } } return $newsarr; } public function fund($key) { $url = "http://japi.juhe.cn/jingzhi/query.from?key={$key}"; $fundarr = $this->https_request($url); $resultarr = $fundarr['result']; // $content = $resultarr['title']; // return $this->replytext($xmlobj,$content); $newsarr = array(); //判断接口是否获取数据 if($jokearr['error_code'] == 0) { foreach($resultarr as $item) { $newsarr[] = array( "title"=>$item['day'], "description"=>$item['title'], "picurl"=>"", "url"=>"http://www.baidu.com" ); } } return $newsarr; } /** *获取基础支持里面的接口调用凭证access_token并缓存access_token *@return access_token string 接口凭证 **/ public function getaccesstoken() { //获取memcache缓存的access_token $access_token = $this->_memcache_get("access_token"); //如果缓存的access_token失效 if(!$access_token) { //如果失效调用获取接口凭证来获取access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}"; $outoptarr = $this->https_request($url); if(!isset($outoptarr['errcode'])) { //memcache缓存access_token $this->_memcache_set("access_token",$outoptarr['access_token'],7000); return $outoptarr['access_token']; } } return $access_token; } //初始化memcache private function _memcache_init() { $mmc = new memcache; $ret = $mmc -> connect(); if ($ret == false) { return; } return $mmc; } //设置memcache private function _memcache_set($key,$value,$time=0) { $mmc = $this->_memcache_init(); $mmc -> set($key,$value,0,$time); } //获取memcahce private function _memcache_get($key) { $mmc = $this->_memcache_init(); return $mmc -> get($key); } //自定义菜单创建 public function menu_create($data) { $access_token = $this->getaccesstoken(); //自定义菜单创建接口地址 $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}"; return $this->https_request($url,$data); } //自定义菜单删除 public function menu_delete() { $access_token = $this->getaccesstoken(); $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={$access_token}"; return $this->https_request($url); } } ?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论