版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/php_lzr/article/details/86072976
我这里请求的接口是接口 B:适用于需要的码数量极多的业务场景 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html这个是官方给的接口文档 下面是我的代码 需要注意的是accesstoken 存起来 设置过期时间 我这边是存在redis中 public static function wxapp_code($accesstoken,$address){ //构建请求二维码参数 //path是扫描二维码跳转的小程序路径,可以带参数?id=xxx //width是二维码宽度 $qcode ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accesstoken"; $param = json_encode(array("scene"=>$address[1],"page"=>$address[0],"width"=> 80)); //POST参数 $result = self::httpRequest( $qcode, $param,"POST"); return $result; }
//把请求发送到微信服务器换取二维码 public static function httpRequest($url, $data='', $method='GET'){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 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, 30); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); return $result; }
//重新获取微信小程序access_token保证唯一性 public static function access_token(){ // 配置APPID、APPSECRET $APPID = WEIXIN_OPENCOURSE_APPID; $APPSECRET = WEIXIN_OPENCOURSE_APPSECRET; //获取access_token $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET"; //缓存access_token $redis = SayRedis::get_instance('lists'); //存俩个小时 $operate_time = time(); $json = self::httpRequest( $access_token ); $json = json_decode($json,true); $redis->set('wxxcx_token'.WEIXIN_OPENCOURSE_APPID,$json["access_token"]);//access_token $redis->set('wxxcx_time',$operate_time);//access_token $accesstoken = $redis->get('wxxcx_token'.WEIXIN_OPENCOURSE_APPID);//access_token return $accesstoken; }