php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
程序员文章站
2023-02-25 20:54:46
微信入口绑定,微信事件处理,微信api全部操作包含在这些文件中。
内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/jsap...
微信入口绑定,微信事件处理,微信api全部操作包含在这些文件中。
内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/jsapi
<?php class wxapi { const appid = ""; const appsecret = ""; const mchid = ""; //商户号 const privatekey = ""; //私钥 public $parameters = array(); public $jsapiticket = null; public $jsapitime = null; 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; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $result = $this->wxhttpsrequest($url); //print_r($result); $jsoninfo = json_decode($result, true); $access_token = $jsoninfo["access_token"]; return $access_token; } /**************************************************** * 微信获取apiticket 返回指定微信公众号的at信息 ****************************************************/ public function wxjsapiticket($appid = null , $appsecret = null){ $appid = is_null($appid) ? self::appid : $appid; $appsecret = is_null($appsecret) ? self::appsecret : $appsecret; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->wxaccesstoken(); $result = $this->wxhttpsrequest($url); $jsoninfo = json_decode($result, true); $ticket = $jsoninfo['ticket']; //echo $ticket . "<br />"; return $ticket; } public function wxverifyjsapiticket($appid = null , $appsecret = null){ if(!empty($this->jsapitime) && intval($this->jsapitime) > time() && !empty($this->jsapiticket)){ $ticket = $this->jsapiticket; } else{ $ticket = $this->wxjsapiticket($appid,$appsecret); $this->jsapiticket = $ticket; $this->jsapitime = time() + 7200; } 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; } /**************************************************** * 微信通过指定模板信息发送给指定用户,发送完成后返回指定json数据 ****************************************************/ public function wxsendtemplate($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); return $result; } /**************************************************** * 发送自定义的模板消息 ****************************************************/ 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 = urldecode(json_encode($template)); echo $jsondata; $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; } /**************************************************** * 微信客服接口 - add 添加客服人员 ****************************************************/ public function wxserviceadd($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服接口 - update 编辑客服人员 ****************************************************/ public function wxserviceupdate($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服接口 - delete 删除客服人员 ****************************************************/ public function wxservicedelete($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信客服接口 - 上传头像 *******************************************************/ public function wxserviceupdatecover($kf_account, $media = '') { $wxaccesstoken = $this->wxaccesstoken(); //$data['access_token'] = $wxaccesstoken; $data['media'] = '@d:\\workspace\\htdocs\\yky_test\\logo.jpg'; $url = "https:// api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=".$wxaccesstoken."&kf_account=".$kf_account; $result = $this->wxhttpsrequest($url,$data); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服接口 - 获取客服列表 ****************************************************/ public function wxservicelist(){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服接口 - 获取在线客服接待信息 ****************************************************/ public function wxserviceonlinelist(){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服接口 - 客服发送信息 ****************************************************/ public function wxservicesend($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服会话接口 - 创建会话 ****************************************************/ public function wxservicesessionadd($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfsession/create?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服会话接口 - 关闭会话 ****************************************************/ public function wxservicesessionclose(){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfsession/close?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服会话接口 - 获取会话 ****************************************************/ public function wxservicesessionget($openid){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfsession/getsession?access_token=".$wxaccesstoken."&openid=" . $openid; $result = $this->wxhttpsrequest($url); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服会话接口 - 获取会话列表 ****************************************************/ public function wxservicesessionlist($kf_account){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?access_token=".$wxaccesstoken."&kf_account=" . $kf_account ; $result = $this->wxhttpsrequest($url); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信客服会话接口 - 未接入会话 ****************************************************/ public function wxservicesessionwaitcase(){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/customservice/kfsession/getwaitcase?access_token=".$wxaccesstoken; $result = $this->wxhttpsrequest($url); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 申请设备id ****************************************************/ public function wxdeviceapply($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/device/applyid?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 编辑设备id ****************************************************/ public function wxdeviceupdate($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/device/update?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 本店关联设备 ****************************************************/ public function wxdevicebindlocation($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/device/bindlocation?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 查询设备列表 ****************************************************/ public function wxdevicesearch($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/device/search?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 新增页面 ****************************************************/ public function wxpageadd($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/page/add?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 编辑页面 ****************************************************/ public function wxpageupdate($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/page/update?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 查询页面 ****************************************************/ public function wxpagesearch($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/page/search?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 删除页面 ****************************************************/ public function wxpagedelete($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/page/delete?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信摇一摇 - 上传图片素材 *******************************************************/ public function wxmaterialadd($media = '') { $wxaccesstoken = $this->wxaccesstoken(); //$data['access_token'] = $wxaccesstoken; $data['media'] = '@d:\\workspace\\htdocs\\yky_test\\logo.jpg'; $url = "https://api.weixin.qq.com/shakearound/material/add?access_token=".$wxaccesstoken; $result = $this->wxhttpsrequest($url,$data); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 配置设备与页面的关联关系 ****************************************************/ public function wxdevicebindpage($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/device/bindpage?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 获取摇周边的设备及用户信息 ****************************************************/ public function wxgetshakeinfo($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /**************************************************** * 微信摇一摇 - 以设备为维度的数据统计接口 ****************************************************/ public function wxgetshakestatistics($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/shakearound/statistics/device?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /***************************************************** * 生成随机字符串 - 最长为32位字符串 *****************************************************/ public function wxnoncestr($length = 16, $type = false) { $chars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } if($type == true){ return strtoupper(md5(time() . $str)); } else { return $str; } } /******************************************************* * 微信商户订单号 - 最长28位字符串 *******************************************************/ public function wxmchbillno($mchid = null) { if(is_null($mchid)){ if(self::mchid == "" || is_null(self::mchid)){ $mchid = time(); } else{ $mchid = self::mchid; } } else{ $mchid = substr(addslashes($mchid),0,10); } return date("ymd",time()).time().$mchid; } /******************************************************* * 微信格式化数组变成参数格式 - 支持url加密 *******************************************************/ public function wxsetparam($parameters){ if(is_array($parameters) && !empty($parameters)){ $this->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->wxverifyjsapiticket(); // 注意 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; } /******************************************************* * 微信卡券:jsapi 卡券package - 基础参数没有附带任何值 - 再生产环境中需要根据实际情况进行修改 *******************************************************/ public function wxcardpackage($cardid , $timestamp = ''){ $api_ticket = $this->wxverifyjsapiticket(); if(!empty($timestamp)){ $timestamp = $timestamp; } else{ $timestamp = time(); } $arrays = array(self::appsecret,$timestamp,$cardid); sort($arrays , sort_string); //print_r($arrays); //echo implode("",$arrays)."<br />"; $string = sha1(implode($arrays)); //echo $string; $resultarray['cardid'] = $cardid; $resultarray['cardext'] = array(); $resultarray['cardext']['code'] = ''; $resultarray['cardext']['openid'] = ''; $resultarray['cardext']['timestamp'] = $timestamp; $resultarray['cardext']['signature'] = $string; //print_r($resultarray); return $resultarray; } /******************************************************* * 微信卡券:jsapi 卡券全部卡券 package *******************************************************/ public function wxcardallpackage($cardidarray = array(),$timestamp = ''){ $rearrays = array(); if(!empty($cardidarray) && (is_array($cardidarray) || is_object($cardidarray))){ //print_r($cardidarray); foreach($cardidarray as $value){ //print_r($this->wxcardpackage($value,$openid)); $rearrays[] = $this->wxcardpackage($value,$timestamp); } //print_r($rearrays); } else{ $rearrays[] = $this->wxcardpackage($cardidarray,$timestamp); } return strval(json_encode($rearrays)); } /******************************************************* * 微信卡券:获取卡券列表 *******************************************************/ public function wxcardlistpackage($cardtype = "" , $cardid = ""){ //$api_ticket = $this->wxverifyjsapiticket(); $resultarray = array(); $timestamp = time(); $noncestr = $this->wxnoncestr(); //$strings = $arrays = array(self::appid,self::appsecret,$timestamp,$noncestr); sort($arrays , sort_string); $string = sha1(implode($arrays)); $resultarray['app_id'] = self::appid; $resultarray['card_sign'] = $string; $resultarray['time_stamp'] = $timestamp; $resultarray['nonce_str'] = $noncestr; $resultarray['card_type'] = $cardtype; $resultarray['card_id'] = $cardid; return $resultarray; } /******************************************************* * 将数组解析xml - 微信红包接口 *******************************************************/ public function wxarraytoxml($parameters = null){ if(is_null($parameters)){ $parameters = $this->parameters; } if(!is_array($parameters) || empty($parameters)){ die("参数不为数组无法解析"); } $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)) { $xml.="<".$key.">".$val."</".$key.">"; } else $xml.="<".$key."><![cdata[".$val."]]></".$key.">"; } $xml.="</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 wxbatchget($offset = 0, $count = 0){ $jsondata = json_encode(array('offset' => intval($offset) , 'count' => intval($count))); $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/location/batchget?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $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; } /******************************************************* * 微信卡券:查询卡券详情 *******************************************************/ public function wxcardgetinfo($jsondata) { $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/get?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信卡券:设置白名单 *******************************************************/ public function wxcardwhitelist($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信卡券:消耗卡券 *******************************************************/ public function wxcardconsume($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/code/consume?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信卡券:删除卡券 *******************************************************/ public function wxcarddelete($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/delete?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信卡券:选择卡券 - 解析code *******************************************************/ public function wxcarddecryptcode($jsondata){ $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/code/decrypt?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信卡券:更改库存 *******************************************************/ public function wxcardmodifystock($cardid , $increase_stock_value = 0 , $reduce_stock_value = 0){ if(intval($increase_stock_value) == 0 && intval($reduce_stock_value) == 0){ return false; } $jsondata = json_encode(array("card_id" => $cardid , 'increase_stock_value' => intval($increase_stock_value) , 'reduce_stock_value' => intval($reduce_stock_value))); $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/modifystock?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } /******************************************************* * 微信卡券:查询用户code *******************************************************/ public function wxcardquerycode($code , $cardid = ''){ $jsondata = json_encode(array("code" => $code , 'card_id' => $cardid )); $wxaccesstoken = $this->wxaccesstoken(); $url = "https://api.weixin.qq.com/card/code/get?access_token=" . $wxaccesstoken; $result = $this->wxhttpsrequest($url,$jsondata); $jsoninfo = json_decode($result, true); return $jsoninfo; } }
分享实例:
<?php require_once 'lib.inc.php'; $wx = new wxapi(); if(!isset($_get['code'])){ header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=".wxapi::appid."&redirect_uri=http://".$_server['server_name'].$_server['php_self']."&response_type=code&scope=snsapi_base&state=1#wechat_redirect"); } else{ $code = $_get['code']; $info = $wx->wxoauthaccesstoken($code); //print_r($info); $openid = $info['openid']; } $signpackage = $wx->wxjsapipackage(); ?> <html> <head> <title>jsapi接口测试</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script> wx.config({ debug: true, appid: '<?php echo $signpackage["appid"];?>', timestamp: <?php echo $signpackage["timestamp"];?>, noncestr: '<?php echo $signpackage["noncestr"];?>', signature: '<?php echo $signpackage["signature"];?>', jsapilist: [ // 所有要调用的 api 都要加到这个列表中 'onmenusharetimeline', 'onmenushareappmessage' ] }); wx.ready(function () { // 在这里调用 api wx.onmenushareappmessage({ title: '互联网之子', desc: '在长大的过程中,我才慢慢发现,我身边的所有事,别人跟我说的所有事,那些所谓本来如此,注定如此的事,它们其实没有非得如此,事情是可以改变的。更重要的是,有些事既然错了,那就该做出改变。', link: 'http://movie.douban.com/subject/25785114/', imgurl: 'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg', trigger: function (res) { // 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回 alert('用户点击发送给朋友'); }, success: function (res) { alert('已分享'); }, cancel: function (res) { alert('已取消'); }, fail: function (res) { alert(json.stringify(res)); } }); wx.onmenusharetimeline({ title: '互联网之子', link: 'http://movie.douban.com/subject/25785114/', imgurl: 'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg', trigger: function (res) { // 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回 alert('用户点击分享到朋友圈'); }, success: function (res) { alert('已分享'); }, cancel: function (res) { alert('已取消'); }, fail: function (res) { alert(json.stringify(res)); } }); }); var readyfunc = function onbridgeready() { document.queryselector('#batchaddcard').addeventlistener('click',function(e) { weixinjsbridge.invoke('batchaddcard', { "card_list": [<?php echo $kqinfo;?>] }, function(res) { alert(res.err_msg + " _ " + res.card_list); }); }); } if (typeof weixinjsbridge === "undefined") { document.addeventlistener('weixinjsbridgeready', readyfunc, false); } else { readyfunc(); } </script> </head> <body> <div> <input type="button" id="batchaddcard" name="batchaddcard" value="添加卡券" /> </div> </body> </html>
<?php /** * description of wechat * * @author administrator */ class wechat extends wxapi{ public $token = ""; //put your code here public function __construct($token = "") { parent::__construct(); $this->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 rule sort($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 , option if($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 'kf_create_session'://创建会话 break; case 'kf_close_session'://关闭会话 break; case 'kf_switch_session'://转接会话 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 = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[%s]]></msgtype> <content><![cdata[%s]]></content> <funcflag>0</funcflag> </xml>"; 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; } }
以上就是为大家分享的全部内容,希望对大家的学习有所帮助。
上一篇: 七言杂咏,工区小菜园