




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
微信各种类库整理index.php1<?php2include_once'lib.inc.php';34$wcObj=newWeChat("YOUKUIYUAN");5$wcObj->wcValid();微信类⼊⼝验证及回复类auth.php1<?php2/********************************************************3456789*****@authorKylerYou<QQ:2444756311>@link/wiki/home/index.html@version2.0.1@uses$wxApi=newWxApi();@package微信API接⼝陆续会继续进⾏更新********************************************************/10classWxApi{111213141516//constappId//constappSecret="";constappId="";constappSecret="";="";//constmchid="";//商户号//constprivatekey="";//私钥17181920212223242526272829303132333435363738394041public$parameters=array();publicfunction__construct(){}/*****************************************************微信提交API⽅法,返回微信指定JSON****************************************************/publicfunctionwxHttpsRequest($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;}424344454647484950515253545556575859606162636465/*****************************************************微信带证书提交数据-微信红包使⽤****************************************************/publicfunctionwxHttpsRequestPem($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,'8');//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');66676869707172737475767778798081828384858687888990curl_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"callfaild,errorCode:$error\n";curl_close($ch);returnfalse;}91}9293/*****************************************************微信获取AccessToken返回指定微信公众号的at信息****************************************************/94959697publicfunctionwxAccessToken($appId=NULL,$appSecret=NULL){98$appId=is_null($appId)?self::appId:$appId;99$appSecret=is_null($appSecret)?self::appSecret:$appSecret;100101102103104$data=json_decode(file_get_contents("access_token.json"));if($data->expire_time<time()){//echo$appId,$appSecret;$url="/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;105>wxHttpsRequest($url);$result=$this-106107108109//print_r($result);$jsoninfo=json_decode($result,true);$access_token=$jsoninfo["access_token"];if($access_token){110111112113$data->expire_time=time()+7000;$data->access_token=$access_token;$fp=fopen("access_token.json","w");fwrite($fp,json_encode($data));114115116117118119120121122123124125126127128129130131132133fclose($fp);}}else{$access_token=$data->access_token;}return$access_token;}/*****************************************************微信获取AccessToken返回指定微信公众号的at信息****************************************************/publicfunctionwxJsApiTicket($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<time()){$url="/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->wxAccessToken();134135136137$result=$this->wxHttpsRequest($url);=json_decode($result,true);$jsoninfo$ticket=$jsoninfo['ticket'];if($ticket){138139140141142143144145146147148149150151152153154155156157$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获取⽤户信息,返回数组****************************************************/publicfunctionwxGetUser($openId){$wxAccessToken=$this->wxAccessToken();$url="/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN";158159160161$result=$this->wxHttpsRequest($url);=json_decode($result,true);$jsoninforeturn$jsoninfo;}162163164165166167168169170171172173174175176177178179180181182183184185186/*****************************************************微信⽣成⼆维码ticket****************************************************/publicfunctionwxQrCodeTicket($jsonData){$wxAccessToken=$this->wxAccessToken();$url="/cgi-bin/qrcode/create?access_token=".$wxAccessToken;=$this->wxHttpsRequest($url,$jsonData);$resultreturn$result;}/*****************************************************微信通过ticket⽣成⼆维码****************************************************/publicfunctionwxQrCode($ticket){$url="/cgi-bin/showqrcode?ticket=".urlencode($ticket);return$url;}/*****************************************************发送⾃定义的模板消息****************************************************/publicfunctionwxSetSend($touser,$template_id,$url,$data,$topcolor='#7B68EE'){187188189190191192193194195196197198199200$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//验证时不返回确认页⾯,只能获取OPENID201202203204205****************************************************/publicfunctionwxOauthBase($redirectUrl,$state="",$appId=NULL){=is_null($appId)?self::appId:$appId;$url="/connect/oauth2/authorize?$appIdappid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect";206return$url;207208209210}/*****************************************************微信设置OAUTH跳转URL,返回字符串信息-SCOPE=snsapi_userinfo//获取⽤户完整信息211212213214215****************************************************/publicfunctionwxOauthUserinfo($redirectUrl,$state="",$appId=NULL){$appId=is_null($appId)?self::appId:$appId;$url="/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";216return$url;217218219220221222223224225226227228229230231232}/*****************************************************微信OAUTH跳转指定URL****************************************************/publicfunctionwxHeader($url){header("location:".$url);}/*****************************************************微信通过OAUTH返回页⾯中获取AT信息****************************************************/publicfunctionwxOauthAccessToken($code,$appId=NULL,$appSecret=NULL){$appId=is_null($appId)?self::appId:$appId;233$appSecret=is_null($appSecret)?self::appSecret:$appSecret;234$url="/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";235$result236=$this->wxHttpsRequest($url);//print_r($result);237$jsoninfo=json_decode($result,true);238//$access_token=$jsoninfo["access_token"];return$jsoninfo;239240}241242/****************************************************243*微信通过OAUTH的Access_Token的信息获取当前⽤户信息//只执⾏在snsapi_userinfo模式运⾏244245246247****************************************************/publicfunctionwxOauthUser($OauthAT,$openId){$url="/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN";248249250251252253254255256$result=$this->wxHttpsRequest($url);=json_decode($result,true);$jsoninforeturn$jsoninfo;}/*****************************************************创建⾃定义菜单****************************************************/257258259260261262263264265266267268269270271272273274275276277278279280281publicfunctionwxMenuCreate($jsonData){$wxAccessToken=$this->wxAccessToken();$url="/cgi-bin/menu/create?access_token=".$wxAccessToken;$result$jsoninfo=$this->wxHttpsRequest($url,$jsonData);=json_decode($result,true);return$jsoninfo;}/*****************************************************获取⾃定义菜单****************************************************/publicfunctionwxMenuGet(){$wxAccessToken=$this->wxAccessToken();$url="/cgi-bin/menu/get?access_token=".$wxAccessToken;=$this->wxHttpsRequest($url);$result$jsoninfo=json_decode($result,true);return$jsoninfo;}/*****************************************************删除⾃定义菜单****************************************************/publicfunctionwxMenuDelete(){282283284285286287288289290291292293294295$wxAccessToken=$this->wxAccessToken();$url="/cgi-bin/menu/delete?access_token=".$wxAccessToken;=$this->wxHttpsRequest($url);$result$jsoninfo=json_decode($result,true);return$jsoninfo;}/*****************************************************获取第三⽅⾃定义菜单****************************************************/publicfunctionwxMenuGetInfo(){$wxAccessToken=$this->wxAccessToken();$url="/cgi-bin/get_current_selfmenu_info?access_token=".$wxAccessToken;296297298299300301302303304305$result=$this->wxHttpsRequest($url);=json_decode($result,true);$jsoninforeturn$jsoninfo;}/******************************************************⽣成随机字符串-最长为32位字符串*****************************************************/publicfunctionwxNonceStr($length=16,$type=FALSE){$chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";306307308309310311312313314315316317318319320321322323324325326327328329330$str="";for($i=0;$i<$length;$i++){$str.=substr($chars,mt_rand(0,strlen($chars)-1),1);}if($type==TRUE){returnstrtoupper(md5(time().$str));}else{return$str;}}/********************************************************微信商户订单号-最长28位字符串*******************************************************/publicfunctionwxMchBillno($mchid=NULL){if(is_null($mchid)){if(self::mchid==""||is_null(self::mchid)){$mchid=time();}else{$mchid=self::mchid;}}331332333334335336337338339340341342343344345346347348349350351352353354else{$mchid=substr(addslashes($mchid),0,10);}returndate("Ymd",time()).time().$mchid;}/********************************************************微信格式化数组变成参数格式-⽀持url加密*******************************************************/publicfunctionwxSetParam($parameters){if(is_array($parameters)&&!empty($parameters)){$this->parameters=$parameters;return$this->parameters;}else{returnarray();}}/********************************************************微信格式化数组变成参数格式-⽀持url加密*******************************************************/355publicfunctionwxFormatArray($parameters=NULL,$urlencode=FALSE){356357358359360361362363364365366367368369370371372373}374375376377378if(is_null($parameters)){$parameters=$this->parameters;}$restr="";//初始化空ksort($parameters);//排序参数foreach($parametersas$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⽅法]*******************************************************/publicfunctionwxMd5Sign($content,$privatekey){379380381382383384385386387388389390391392393394395396397398399400401402403try{if(is_null($privatekey)){thrownewException("财付通签名key不能为空!");}if(is_null($content)){thrownewException("财付通签名内容不能为空");}$signStr=$content."&key=".$privatekey;returnstrtoupper(md5($signStr));}catch(Exception$e){die($e->getMessage());}}/********************************************************微信Sha1签名⽣成器-需要将参数数组转化成为字符串[wxFormatArray⽅法]*******************************************************/publicfunctionwxSha1Sign($content){try{if(is_null($content)){thrownewException("签名内容不能为空");}//$signStr=$content;404405406407408409410411412413414415416417418419returnsha1($content);}catch(Exception$e){die($e->getMessage());}}/********************************************************微信jsApi整合⽅法-通过调⽤此⽅法获得jsapi数据*******************************************************/publicfunctionwxJsapiPackage(){$jsapi_ticket=$this->wxJsApiTicket();//注意URL⼀定要动态获取,不能hardcode.$protocol=(!empty($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=='off'||$_SERVER['SERVER_PORT']==443)?"https://":"http://";420$url=$protocol.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];421422423424425426427$timestamp=time();$nonceStr=$this->wxNonceStr();$signPackage=array("jsapi_ticket"=>$jsapi_ticket,"nonceStr"=>$nonceStr,428429430431432433434435436437438439440441442443444445446447448449450451452"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-微信红包接⼝*******************************************************/publicfunctionwxArrayToXml($parameters=NULL){if(is_null($parameters)){$parameters=$this->parameters;}453454455456457458459460461462463464465466467468469470471472473474475476if(!is_array($parameters)||empty($parameters)){die("参数不为数组⽆法解析");}$xml="<xml>";foreach($arras$key=>$val){if(is_numeric($val)){$xml.="<".$key.">".$val."</".$key.">";}else$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";}$xml.="</xml>";return$xml;}/********************************************************微信卡券:上传LOGO-需要改写动态功能*******************************************************/publicfunctionwxCardUpdateImg(){$wxAccessToken=$this->wxAccessToken();477478//$data['access_token']=$wxAccessToken;$data['buffer']='@D:\\workspace\\htdocs\\yky_test\\logo.jpg';479480481482483$url="/cgi-bin/media/uploadimg?access_token=".$wxAccessToken;=$this->wxHttpsRequest($url,$data);$result$jsoninfo=json_decode($result,true);return$jsoninfo;//array(1){["url"]=>string(121)"/mmbiz/ibuYxPHqeXePNTW4ATKyias1Cf3zTKiars9PFPzF1k5icvXD7xW0kXUAxHDzkEPd9micCMCN0dcTJfW6Tnm93MiaAfRQ/0"}484485486487488489490491492493494495496497498}/********************************************************微信卡券:获取颜⾊*******************************************************/publicfunctionwxCardColor(){$wxAccessToken=$this->wxAccessToken();$url="/card/getcolors?access_token=".$wxAccessToken;=$this->wxHttpsRequest($url);$result$jsoninfo=json_decode($result,true);return$jsoninfo;}/********************************************************微信卡券:创建卡券499500501502503504505506507508509*******************************************************/publicfunctionwxCardCreated($jsonData){$wxAccessToken=$this->wxAccessToken();$url="/card/create?access_token=".$wxAccessToken;=$this->wxHttpsRequest($url,$jsonData);$result$jsoninfo=json_decode($result,true);return$jsoninfo;}/********************************************************微信卡券:JSAPI卡券Package-基础参数没有附带任何值-再⽣产环境中需要根据实际情况进⾏修改510511512513514515516517518519520521522*******************************************************/publicfunctionwxCardPackage($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;523$resultArray['card_ext']['signature']=$string;return$resultArray;524525526}527528529}微信JSAPI1<?php2require_once'lib.inc.php';3$wx=newWxApi();4//通过⽹页获取openid5//if(!isset($_GET['code'])){6//header("location:/connect/oauth2/authorize?appid=".WxApi::appId."&redirect_uri=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."&response_type=code&scope=snsapi_base&state=1#we7//}8//else{9//$CODE=$_GET['code'];10//$Info=$wx->wxOauthAccessToken($CODE);11//print_r($Info);12//$openId=$Info['openid'];13//}14////////////////////////////////////////////1516$signPackage=$wx->wxJsapiPackage();17//print_r($signPackage);18$kqInfo=$wx->wxCardPackage("");19$listInfo=$wx->wxCardListPackage();20?>21<html>22<head>232425262728<title>JSAPI接⼝测试</title><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><scriptsrc="/jquery-1.11.2.min.js"></script><scriptsrc="/open/js/jweixin-1.0.0.js"></script>29</head>30<body>31323334353637383940<div><inputtype="button"id="batchAddCard"name="batchAddCard"value="添加卡券"/><br/><inputtype="button"id="openCard"name="openCard"value="拉起卡券库"/><br/><inputtype="button"id="ShareTimeLine"name="ShareTimeLine"value="分享朋友圈"/><br/><divid="showInfo"></div></div><script>4142434445464748495051525354555657585960wx.config({debug:false,appId:'<?phpecho$signPackage["appId"];?>',timestamp:<?phpecho$signPackage["timestamp"];?>,nonceStr:'<?phpecho$signPackage["nonceStr"];?>',signature:'<?phpecho$signPackage["signature"];?>',jsApiList:[//所有要调⽤的API都要加到这个列表中'onMenuShareTimeline','onMenuShareAppMessage','addCard','openCard']});wx.ready(function(){//在这⾥调⽤APIwx.onMenuShareAppMessage({title:'互联⽹之⼦',desc:'在长⼤的过程中,我才慢慢发现,我⾝边的所有事,别⼈跟我说的所有事,那些所谓本来如此,注定如此的事,它们其实没有⾮得如此,事情是可以改变的。更重要的是,有些事既然错了,那就该做出改变。616263link:'/subject/25785114/',imgUrl:'/jssdk/images/p2166127561.jpg',trigger:function(res){64//不要尝试在trigger中使⽤ajax异步请求修改本次分享的内容,因为客户端分享操作是⼀个同步操作,这时候使⽤ajax的回包会还没有返回6566676869alert('⽤户点击发送给朋友');},success:function(res){alert('已分享');},707172737475767778798081828384cancel:function(res){alert('已取消');},fail:function(res){alert(JSON.stringify(res));}});document.querySelector('#ShareTimeLine').onclick=function(){wx.onMenuShareTimeline({title:'互联⽹之⼦',link:'/subject/25785114/',imgUrl:'/jssdk/images/p2166127561.jpg',trigger:function(res){//不要尝试在trigger中使⽤ajax异步请求修改本次分享的内容,因为客户端分享操作是⼀个同步操作,这时候使⽤ajax的回包会还没有返回85alert('⽤户点击分享到朋友圈');86},87success:function(res){88alert('已分享');89},90cancel:function(res){91alert('已取消');92},93fail:function(res){94alert(JSON.stringify(res));}9596});97};9899document.querySelector('#batchAddCard').onclick=function(){100101102103104wx.addCard({cardList:[{cardId:'***********************',cardExt:'{"timestamp":"<?phpecho$kqInfo['cardExt']['timestamp'];?>","signature":"<?phpecho$kqInfo['cardExt']['signature'];?>"}'105106107108109}],success:function(res){varcardList=res.cardList;//添加的卡券列表信息alert(cardList);110111112113114115116117118119120121122123124},cancel:function(res){alert('已取消');},fail:function(res){alert(JSON.stringify(res));}});};varshareData={title:'微信JS-SDKDemo',desc:'微信JS-SDK,帮助第三⽅为⽤户提供更优质的移动web服务',link:'/jssdk/',imgUrl:'/mmbiz/icTdbqWNOwNRt8Qia4lv7k3M9J1SKqKCImxJCt7j9rHYicKDI45jRPBxdzdyREWnk0ia0N5TMnMfth7SdxtzMvVgXg/0'125};126127128129130131132133wx.onMenuShareAppMessage(shareData);wx.onMenuShareTimeline(shareData);});varreadyFunc=func
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 呼伦贝尔学院《英语教学名家教学赏析》2023-2024学年第二学期期末试卷
- 2017小学生消防课件
- 一流课程汇报答辩
- 泰山护理职业学院《基础俄语Ⅲ》2023-2024学年第一学期期末试卷
- 甘肃省2025届数学三下期末调研试题含解析
- 星海音乐学院《小动物临床用药专题》2023-2024学年第二学期期末试卷
- 浙江省宁波市北仑区部分校2024-2025学年小升初数学检测卷含解析
- 石家庄学院《建筑审美与评论》2023-2024学年第二学期期末试卷
- 梅州市蕉岭县2025年数学四下期末综合测试试题含解析
- 西安电子科技大学长安学院《药物合成原理》2023-2024学年第二学期期末试卷
- 平行四边形的应用动点问题
- 客户受电工程竣工检验意见书(南网)
- 多媒体课件制作流程图
- MT_T 695-1997 煤矿用高倍数泡沫灭火剂通用技术条件_(高清版)
- 《商业发票》word版
- 土地使用权(住宅用地)市场比较法评估测算表
- DFMEA全解(完整版)
- 《教案封面设计》word版
- 奥迪A4L汽车驱动桥的结构设计毕业设计
- (最新整理)世界水利发展史
- 超市新员工进职[新版]ppt课件
评论
0/150
提交评论