微信官方提供了小程序码的生成接口,支持生成二维码形式的小程序码,或原生的圆形小程序码,这两者在生成的时候仅仅是post接口的url不同,传递的参数都基本相同(提交的post参数需要转成json字符串,不支持 form表单提交)。
本文主要是通过官方提供的接口A和接口C来生成小程序码(提取了两个接口可公共使用的提交参数,所以只要改pos提交的url地址就能实现生成二维码还是原生码了),此情况适用于少量小程序码的生成:
注意事项
接口只能生成已发布的小程序的二维码
接口 A 加上接口 C,总共生成的码数量限制为 100,000,请谨慎调用。
接口 B 调用分钟频率受限(5000次/分钟),如需大量小程序码,建议预生成。
官方文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
下面上php代码,注意我代码里出现的微信小程序相关配置都是测试用的,不是真实的。
<?phpheader('content-type:text/html;charset=utf-8');//配置APPID、APPSECRET$APPID ="wxa51564dfd3";$APPSECRET = "4xfg524t65468j641fs68ry78";//需要生成的带参数小程序码,根据实际情况填写,写入数组是为了方便图片的命名$mend['小程序码path参数1'] = 'pages/index?bey=1&storeId=cc502061';$mend['小程序码path参数2'] = 'pages/main/index?bey=1&storeId=cc288cb7';$mend['小程序码path参数3'] = 'pages/main/index?bey=1&storeId=eb12e4d0';//上面是需要修改的配置项及参数,后面的小程序码会生成在php服务器路的php文件同目录下//缓存access_tokensession_start();//获取access_token$access_token ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";if(!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() >$_SESSION['expires_in'])){$_SESSION['access_token'] ="";$_SESSION['expires_in'] = 0;$json = httpRequest($access_token );$json = json_decode($json,true);// var_dump($json);$_SESSION['access_token'] =$json['access_token'];$_SESSION['expires_in'] = time()+7100;//有效期2小时,设置提前一点7100s$ACCESS_TOKEN =$json["access_token"];}else{$ACCESS_TOKEN = $_SESSION["access_token"];}//接口C 小程序二维码,此接口生成的码有总数量限制与下方url接口共享10w总额度//$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";//接口A 小程序码,此接口生成的码有总数量限制与上方url接口共享10w总额度$qcode ="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$ACCESS_TOKEN;//循环生成带参数小程序码foreach ($mendian as $k => $v){$param = json_encode(array("path"=>$v,"width"=> 1080));// path参数必填 ,width是将要生成的小程序码的宽度//$param = json_encode(array("path"=>"pages/main/index?bey=1&storeId=724ed3b7","width"=> 1080));$result = httpRequest($qcode,$param,"POST");//将参数$param 进行post提交//将微信返回的结果存为图片,如果生成图片失败,微信会返回json数据$errcode = json_decode($result,true)['errcode'];$errmsg = json_decode($result,true)['errmsg'];if($errcode){echo "{"status:"$errcode,"info:"$errmsg}";}else{file_put_contents($k.".png",$result);//存入图片//$base64_image ="data:image/jpeg;base64,".base64_encode($result );//将图片转换为base64格式图片}sleep(5);}echo ‘结束……’;//封装post、get请求的函数// httpRequest($url); // get请求// httpRequest($url,$json_param,"POST");// post请求function httpRequest($url,$data='',$method='GET'){$curl = curl_init();curl_setopt($curl, CURLOPT_URL,$url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //不校验sslcurl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //不校验sslcurl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);//curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($curl, CURLOPT_AUTOREFERER, 1);if($method=='POST'){curl_setopt($curl, CURLOPT_POST, 1);if ($data !=''){curl_setopt($curl, CURLOPT_POSTFIELDS,$data);}}curl_setopt($curl, CURLOPT_TIMEOUT, 15); //超时时间15scurl_setopt($curl, CURLOPT_HEADER, 0);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$result = curl_exec($curl);curl_close($curl);return $result;}?>
其中要注意的就是php在post请求微信生码接口后如果成功,微信返回的是图片 Buffer,需要使用file_put_contents函数直接输出到图片文件!
发表评论