微信小程序生成二维码+适合推广下线
先献上API文档 —-传送门
分两部分,1获取token,2用token+post数据过去,返回二维码
后面附上存服务器方法,主要传给小程序看
<?php //本教程提取于thinkphp,用作其他框架或者原生,修改操作数据库跟接收的数据即可 //用微信id和秘钥获取token,存数据库 $wxId = 'wx48888888888888'; $wxSecret = 'a6ead8efa94186d55e5788888888888888'; header('Content-Type:image/jpg');//防乱码 //ob_end_clean();//如果还有乱码,请解放我 date_default_timezone_set('PRC'); $times =time(); $access_token = db("access_token"); //先向数据库提取access_token的时间判断是否超过7200s $tokenlist = $access_token ->where('id',1)-> find(); $timeLong = $times -$tokenlist['time'];//当前时间-数据库时间看token是否过期 if($tokenlist == null||$timeLong > 7200){ $getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$wxId&secret=$wxSecret"; // get请求 $result = file_get_contents($getToken); // json转成数组 $arrResult = json_decode($result, TRUE); // 获得token $token = $arrResult['access_token']; $arr = array('token' => $token,'time' =>$times ); //把获取到token更新到数据库 $access_token ->where('id',1)-> update($arr); }else{ $token =$tokenlist['token']; } $id = input('param.id');//接收的id $data_string = array( 'scene' => "$id");//传的值,参考api文档 $data_string = json_encode($data_string); $ch = curl_init("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$token");//接口B,看业务需求选 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($ch, CURLOPT_ENCODING, ""); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string), )); $result = curl_exec($ch); if (curl_errno($ch)) { print curl_error($ch); } curl_close($ch); echo $result;
如果返回是乱码,请加上ob_end_clean();
再弄个精简版,可以直接用的,不保存token的
<?php $wxId = 'wx40588888'; $wxSecret = 'a6ead8efa9488888888888a'; header('Content-Type:image/jpg');//防乱码 date_default_timezone_set('PRC'); $getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$wxId&secret=$wxSecret"; $result = file_get_contents($getToken); $arrResult = json_decode($result, TRUE); // 获得token $token = $arrResult['access_token']; $data_string = json_encode(array( 'scene' => 20));//传的值,参考api文档 $ch = curl_init("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$token");//接口B,看业务需求选 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string), )); $result = curl_exec($ch); if (curl_errno($ch)) { print curl_error($ch); } curl_close($ch); echo $result;
如果现在小程序上展示这个二维码,还需要保存到服务器先,再json这个图片链接过去
方法,在最后加上
file_put_contents(路径,$result)
file_put_contents(ROOT_PATH . ‘public’ . DS . ‘upload/lower/’.$arr[‘id’].’.jpg’,$result);//thinkphp
echo json_encode(“<img src=’https://域名/upload/lower/”.$arr[‘id’].”.jpg’>”);
升级功能:避免重复更新图片
$a=file_get_contents(‘http://url.jpg’);
if ($a) {
var_dump($a);
}
码字很辛苦,转载请注明来自感触life-博客的《微信小程序生成二维码+适合推广下线》
评论