您好,欢迎访问一九零五行业门户网

微信API接口大全

本文给大家介绍的是个人总结的一些微信api接口,包括微信支付、微信红包、微信卡券、微信小店等,十分的全面,有需要的小伙伴可以参考下。
微信入口绑定,微信事件处理,微信api全部操作包含在这些文件中。
微信支付、微信红包、微信卡券、微信小店。
1. [代码]index.php    
wcvalid();
2. [代码]微信入口类    
token = $token; }public function wcchecksignature(){try{if (empty($this->token)) {throw new exception('token is not defined!');}$signature = $_get[signature];$timestamp = $_get[timestamp];$nonce = $_get[nonce];$token = $this->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;}}catch (exception $e) {echo 'message: ' .$e->getmessage();} }public function wcvalid(){$echostr = isset($_get[echostr]) && !empty($_get[echostr]) ? addslashes($_get[echostr]) : null;if(is_null($echostr)){$this->wcmsg();}else{//valid signature , optionif($this->wcchecksignature()){echo $echostr;exit;}else{exit();}} }public function wcmsg(){//get post data, may be due to the different environments$poststr = isset($globals[http_raw_post_data]) && !empty($globals[http_raw_post_data]) ? $globals[http_raw_post_data] : ;if(!empty($poststr)){libxml_disable_entity_loader(true);$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);$this->zclog(true,$postobj);$fromusername = $postobj->fromusername;$tousername = $postobj->tousername;$msgtype = $postobj->msgtype;if($msgtype == 'event'){//执行事件相应$event = $postobj->event;switch ($event) {case 'subscribe'://关注break;case 'unsubscribe'://取消关注break;case 'scan'://扫描break;case 'location'://地址break;case 'click'://点击时间break;case 'view'://跳转break;case 'card_pass_check'://卡券审核通过break;case 'card_not_pass_check'://卡券审核失败break;case 'user_get_card'://用户领取卡券break;case 'user_del_card'://用户删除卡券break;case 'user_view_card'://用户浏览会员卡break;case 'user_consume_card'://用户核销卡券break;case 'merchant_order'://微小店用户下单付款break;default:break;}}else{switch ($msgtype) {case 'text'://文本格式break;case 'image'://图片格式break;case 'voice'://声音break;case 'video'://视频break;case 'shortvideo'://小视频break;case 'location'://上传地理位置break;case 'link'://链接相应break;default:break;}}////////////////////////////////////////////////////////////////////$keyword = trim($postobj->content);$time = time();$texttpl = %s0;if(!empty( $keyword )){$msgtype = text;$contentstr = welcome to wechat world!;$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);echo $resultstr;}else{echo input something...;}////////////////////////////////////////////////////////////////////}else{echo 暂时没有任何信息!;exit;} }//日志log public function zclog($errcode , $errmsg){$this->returnay = array();$this->returnay['errcode'] = $errcode;$this->returnay['errmsg'] = $errmsg;$this->returnay['errtime'] = date(y-m-d h:i:s,time());$logfile = fopen(logfile_.date(ymd,time())..txt, a+);$txt = json_encode($this->returnay).\n;fwrite($logfile, $txt);fclose($logfile);//return $this->returnay; }}
3. [代码]微信操作类 - 更新了自定义菜单部分    
* @link * @version 2.0.1* @uses $wxapi = new wxapi();* @package 微信api接口 陆续会继续进行更新********************************************************/class wxapi {//const appid= ;//const appsecret = ;const appid= ;const appsecret = ;//const mchid= ; //商户号//const privatekey = ; //私钥public $parameters = array();public function __construct(){}/***************************************************** 微信提交api方法,返回微信指定json****************************************************/public function wxhttpsrequest($url,$data = null){$curl = curl_init();curl_setopt($curl, curlopt_url, $url);curl_setopt($curl, curlopt_ssl_verifypeer, false);curl_setopt($curl, curlopt_ssl_verifyhost, false);if (!empty($data)){curl_setopt($curl, curlopt_post, 1);curl_setopt($curl, curlopt_postfields, $data);}curl_setopt($curl, curlopt_returntransfer, 1);$output = curl_exec($curl);curl_close($curl);return $output;}/***************************************************** 微信带证书提交数据 - 微信红包使用****************************************************/public function wxhttpsrequestpem($url, $vars, $second=30,$aheader=array()){$ch = curl_init();//超时时间curl_setopt($ch,curlopt_timeout,$second);curl_setopt($ch,curlopt_returntransfer, 1);//这里设置代理,如果有的话//curl_setopt($ch,curlopt_proxy, '10.206.30.98');//curl_setopt($ch,curlopt_proxyport, 8080);curl_setopt($ch,curlopt_url,$url);curl_setopt($ch,curlopt_ssl_verifypeer,false);curl_setopt($ch,curlopt_ssl_verifyhost,false);//以下两种方式需选择一种//第一种方法,cert 与 key 分别属于两个.pem文件//默认格式为pem,可以注释curl_setopt($ch,curlopt_sslcerttype,'pem');curl_setopt($ch,curlopt_sslcert,getcwd().'/apiclient_cert.pem');//默认格式为pem,可以注释curl_setopt($ch,curlopt_sslkeytype,'pem');curl_setopt($ch,curlopt_sslkey,getcwd().'/apiclient_key.pem');curl_setopt($ch,curlopt_cainfo,'pem');curl_setopt($ch,curlopt_cainfo,getcwd().'/rootca.pem');//第二种方式,两个文件合成一个.pem文件//curl_setopt($ch,curlopt_sslcert,getcwd().'/all.pem');if( count($aheader) >= 1 ){curl_setopt($ch, curlopt_httpheader, $aheader);}curl_setopt($ch,curlopt_post, 1);curl_setopt($ch,curlopt_postfields,$vars);$data = curl_exec($ch);if($data){curl_close($ch);return $data;}else {$error = curl_errno($ch);echo call faild, errorcode:$error\n;curl_close($ch);return false;}}/***************************************************** 微信获取accesstoken 返回指定微信公众号的at信息****************************************************/public function wxaccesstoken($appid = null , $appsecret = null){$appid= is_null($appid) ? self::appid : $appid;$appsecret = is_null($appsecret) ? self::appsecret : $appsecret;$data = json_decode(file_get_contents(access_token.json));if ($data->expire_time wxhttpsrequest($url);//print_r($result);$jsoninfo = json_decode($result, true);$access_token = $jsoninfo[access_token];if ($access_token) {$data->expire_time = time() + 7000;$data->access_token = $access_token;$fp = fopen(access_token.json, w);fwrite($fp, json_encode($data));fclose($fp);}}else {$access_token = $data->access_token;}return $access_token;}/***************************************************** 微信获取accesstoken 返回指定微信公众号的at信息****************************************************/public function wxjsapiticket($appid = null , $appsecret = null){$appid= is_null($appid) ? self::appid : $appid;$appsecret = is_null($appsecret) ? self::appsecret : $appsecret;$data = json_decode(file_get_contents(jsapi_ticket.json));if ($data->expire_time wxaccesstoken();$result= $this->wxhttpsrequest($url);$jsoninfo = json_decode($result, true);$ticket = $jsoninfo['ticket'];if ($ticket) {$data->expire_time = time() + 7000;$data->jsapi_ticket = $ticket;$fp = fopen(jsapi_ticket.json, w);fwrite($fp, json_encode($data));fclose($fp);}}else {$ticket = $data->jsapi_ticket;}return $ticket;}/***************************************************** 微信通过openid获取用户信息,返回数组****************************************************/public function wxgetuser($openid){$wxaccesstoken = $this->wxaccesstoken();$url= https://api.weixin.qq.com/cgi-bin/user/info?access_token=.$wxaccesstoken.&openid=.$openid.&lang=zh_cn;$result= $this->wxhttpsrequest($url);$jsoninfo = json_decode($result, true);return $jsoninfo;}/***************************************************** 微信生成二维码ticket****************************************************/public function wxqrcodeticket($jsondata){$wxaccesstoken = $this->wxaccesstoken();$url = https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=.$wxaccesstoken;$result= $this->wxhttpsrequest($url,$jsondata);return $result;}/***************************************************** 微信通过ticket生成二维码****************************************************/public function wxqrcode($ticket){$url = https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket= . urlencode($ticket);return $url;}/***************************************************** 发送自定义的模板消息****************************************************/public function wxsetsend($touser, $template_id, $url, $data, $topcolor = '#7b68ee'){$template = array('touser' => $touser,'template_id' => $template_id,'url' => $url,'topcolor' => $topcolor,'data' => $data);$jsondata = json_encode($template);$result = $this->wxsendtemplate($jsondata);return $result;}/***************************************************** 微信设置oauth跳转url,返回字符串信息 - scope = snsapi_base //验证时不返回确认页面,,只能获取openid****************************************************/public function wxoauthbase($redirecturl,$state = ,$appid = null){$appid= is_null($appid) ? self::appid : $appid;$url = https://open.weixin.qq.com/connect/oauth2/authorize?appid=.$appid.&redirect_uri=.$redirecturl.&response_type=code&scope=snsapi_base&state=.$state.#wechat_redirect;return $url;}/***************************************************** 微信设置oauth跳转url,返回字符串信息 - scope = snsapi_userinfo //获取用户完整信息****************************************************/public function wxoauthuserinfo($redirecturl,$state = ,$appid = null){$appid= is_null($appid) ? self::appid : $appid;$url = https://open.weixin.qq.com/connect/oauth2/authorize?appid=.$appid.&redirect_uri=.$redirecturl.&response_type=code&scope=snsapi_userinfo&state=.$state.#wechat_redirect;return $url;}/***************************************************** 微信oauth跳转指定url****************************************************/public function wxheader($url){header(location:.$url);}/***************************************************** 微信通过oauth返回页面中获取at信息****************************************************/public function wxoauthaccesstoken($code,$appid = null , $appsecret = null){$appid= is_null($appid) ? self::appid : $appid;$appsecret = is_null($appsecret) ? self::appsecret : $appsecret;$url = https://api.weixin.qq.com/sns/oauth2/access_token?appid=.$appid.&secret=.$appsecret.&code=.$code.&grant_type=authorization_code;$result= $this->wxhttpsrequest($url);//print_r($result);$jsoninfo = json_decode($result, true);//$access_token = $jsoninfo[access_token];return $jsoninfo;}/***************************************************** 微信通过oauth的access_token的信息获取当前用户信息 // 只执行在snsapi_userinfo模式运行****************************************************/public function wxoauthuser($oauthat,$openid){$url= https://api.weixin.qq.com/sns/userinfo?access_token=.$oauthat.&openid=.$openid.&lang=zh_cn;$result= $this->wxhttpsrequest($url);$jsoninfo = json_decode($result, true);return $jsoninfo;}/***************************************************** 创建自定义菜单****************************************************/public function wxmenucreate($jsondata){$wxaccesstoken = $this->wxaccesstoken();$url= https://api.weixin.qq.com/cgi-bin/menu/create?access_token= . $wxaccesstoken;$result= $this->wxhttpsrequest($url,$jsondata);$jsoninfo = json_decode($result, true);return $jsoninfo;}/***************************************************** 获取自定义菜单****************************************************/public function wxmenuget(){$wxaccesstoken = $this->wxaccesstoken();$url= https://api.weixin.qq.com/cgi-bin/menu/get?access_token= . $wxaccesstoken;$result= $this->wxhttpsrequest($url);$jsoninfo = json_decode($result, true);return $jsoninfo;}/***************************************************** 删除自定义菜单****************************************************/public function wxmenudelete(){$wxaccesstoken = $this->wxaccesstoken();$url= https://api.weixin.qq.com/cgi-bin/menu/delete?access_token= . $wxaccesstoken;$result= $this->wxhttpsrequest($url);$jsoninfo = json_decode($result, true);return $jsoninfo;}/***************************************************** 获取第三方自定义菜单****************************************************/public function wxmenugetinfo(){$wxaccesstoken = $this->wxaccesstoken();$url= https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token= . $wxaccesstoken;$result= $this->wxhttpsrequest($url);$jsoninfo = json_decode($result, true);return $jsoninfo;}/****************************************************** 生成随机字符串 - 最长为32位字符串*****************************************************/public function wxnoncestr($length = 16, $type = false) {$chars = abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789;$str = ;for ($i = 0; $i parameters = $parameters;return $this->parameters;}else{return array();}}/******************************************************** 微信格式化数组变成参数格式 - 支持url加密*******************************************************/public function wxformatarray($parameters = null, $urlencode = false){if(is_null($parameters)){$parameters = $this->parameters;}$restr = ;//初始化空ksort($parameters);//排序参数foreach ($parameters as $k => $v){//循环定制参数if (null != $v && null != $v && sign != $k) {if($urlencode){//如果参数需要增加url加密就增加,不需要则不需要$v = urlencode($v);}$restr .= $k . = . $v . &;//返回完整字符串}}if (strlen($restr) > 0) {//如果存在数据则将最后“&”删除$restr = substr($restr, 0, strlen($restr)-1);}return $restr;//返回字符串 }/******************************************************** 微信md5签名生成器 - 需要将参数数组转化成为字符串[wxformatarray方法]*******************************************************/public function wxmd5sign($content, $privatekey){try {if (is_null($privatekey)) {throw new exception(财付通签名key不能为空!);}if (is_null($content)) {throw new exception(财付通签名内容不能为空);}$signstr = $content . &key= . $privatekey;return strtoupper(md5($signstr));}catch (exception $e){die($e->getmessage());}}/******************************************************** 微信sha1签名生成器 - 需要将参数数组转化成为字符串[wxformatarray方法]*******************************************************/public function wxsha1sign($content){try {if (is_null($content)) {throw new exception(签名内容不能为空);}//$signstr = $content;return sha1($content);}catch (exception $e){die($e->getmessage());}}/******************************************************** 微信jsapi整合方法 - 通过调用此方法获得jsapi数据*******************************************************/public function wxjsapipackage(){$jsapi_ticket = $this->wxjsapiticket();// 注意 url 一定要动态获取,不能 hardcode.$protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? https:// : http://;$url = $protocol.$_server[http_host].$_server[request_uri];$timestamp = time();$noncestr = $this->wxnoncestr();$signpackage = array(jsapi_ticket => $jsapi_ticket,noncestr => $noncestr,timestamp => $timestamp,url => $url);// 这里参数的顺序要按照 key 值 ascii 码升序排序$rawstring = jsapi_ticket=$jsapi_ticket&noncestr=$noncestr×tamp=$timestamp&url=$url;//$rawstring = $this->wxformatarray($signpackage);$signature = $this->wxsha1sign($rawstring);$signpackage['signature'] = $signature;$signpackage['rawstring'] = $rawstring;$signpackage['appid'] = self::appid;return $signpackage;}/******************************************************** 将数组解析xml - 微信红包接口*******************************************************/public function wxarraytoxml($parameters = null){if(is_null($parameters)){$parameters = $this->parameters;}if(!is_array($parameters) || empty($parameters)){die(参数不为数组无法解析);}$xml = ;foreach ($arr as $key=>$val){if (is_numeric($val)){$xml.=.$val..$key.>;}else$xml.=.$key.>;}$xml.=;return $xml;}/******************************************************** 微信卡券:上传logo - 需要改写动态功能*******************************************************/public function wxcardupdateimg() {$wxaccesstoken = $this->wxaccesstoken();//$data['access_token'] = $wxaccesstoken;$data['buffer'] = '@d:\\workspace\\htdocs\\yky_test\\logo.jpg';$url= https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=.$wxaccesstoken;$result= $this->wxhttpsrequest($url,$data);$jsoninfo = json_decode($result, true);return $jsoninfo;//array(1) { [url]=> string(121) http://mmbiz.qpic.cn/mmbiz/ibuyxphqexepntw4atkyias1cf3ztkiars9pfpzf1k5icvxd7xw0kxuaxhdzkepd9miccmcn0dctjfw6tnm93miaafrq/0 }}/******************************************************** 微信卡券:获取颜色*******************************************************/public function wxcardcolor(){$wxaccesstoken = $this->wxaccesstoken();$url= https://api.weixin.qq.com/card/getcolors?access_token=.$wxaccesstoken;$result= $this->wxhttpsrequest($url);$jsoninfo = json_decode($result, true);return $jsoninfo;}/******************************************************** 微信卡券:创建卡券*******************************************************/public function wxcardcreated($jsondata) {$wxaccesstoken = $this->wxaccesstoken();$url= https://api.weixin.qq.com/card/create?access_token= . $wxaccesstoken;$result= $this->wxhttpsrequest($url,$jsondata);$jsoninfo = json_decode($result, true);return $jsoninfo;}/******************************************************** 微信卡券:jsapi 卡券package - 基础参数没有附带任何值 - 再生产环境中需要根据实际情况进行修改*******************************************************/public function wxcardpackage($cardid){$timestamp = time();$api_ticket = $this->wxjsapiticket();$cardid = $cardid;$arrays = array($api_ticket,$timestamp,$cardid);sort($arrays);$string = sha1(implode(,$arrays));$resultarray['card_id'] = $cardid;$resultarray['card_ext'] = array();$resultarray['card_ext']['openid'] = 'oomn4s9miwqhsnnvpn0dbtu23toa';$resultarray['card_ext']['timestamp'] = $timestamp;$resultarray['card_ext']['signature'] = $string;return $resultarray;}}
4. [代码]微信jsapi
其它类似信息

推荐信息