1 <?php 2
3 getwxacode(); 4
5 //生成二维码
6 function getwxacode(){ 7 $url = "https://api.weixin.qq.com/wxa/getwxacode?"; 8 $url .= "access_token=" . getToken(); 9 $postdata = [ 10 "path" => "pages/index/index?clientId=3", 11 "width" => 430, 12 ]; 13
14 $res = curl_post($url,json_encode($postdata),$options=array()); 15 $img = './img/'.time().'.jpg'; 16 $r = file_put_contents($img,$res); 17 echo "<img src='".$img."'>"; 18
19 } 20
21
22 //发送获取token请求,获取token(2小时)
23 function getToken() { 24 $url = getTokenUrlStr(); 25 $res = curl_post($url,$postdata='',$options=array()); 26
27 $data = json_decode($res,JSON_FORCE_OBJECT); 28 return $data['access_token']; 29 } 30
31 //获取token的url参数拼接
32 function getTokenUrlStr() 33 { 34 $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?"; //获取token的url
35 $WXappid = "wx0399845015067c51"; //APPID
36 $WXsecret = "7d7205f85b79805006ecfa2121ef4272"; //secret
37 $str = $getTokenUrl; 38 $str .= "grant_type=client_credential&"; 39 $str .= "appid=" . $WXappid . "&"; 40 $str .= "secret=" . $WXsecret; 41
42 return $str; 43 } 44
45 function curl_post($url='',$postdata='',$options=array()){ 46 $ch=curl_init($url); 47 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 48 curl_setopt($ch,CURLOPT_POST,1); 49 curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 50 curl_setopt($ch, CURLOPT_TIMEOUT, 5); 51 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 52 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 53 if(!empty($options)){ 54 curl_setopt_array($ch, $options); 55 } 56 $data=curl_exec($ch); 57 curl_close($ch); 58 return $data; 59 } 60
61 function curl_get($url='',$options=array()){ 62 $ch=curl_init($url); 63 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 64 curl_setopt($ch,CURLOPT_TIMEOUT,5); 65 if(!empty($options)){ 66 curl_setopt_array($ch,$options); 67 } 68 $data=curl_exec($ch); 69 curl_close($ch); 70 return $data; 71 }