php生成微信小程序二维码

阅读次数: 14,043

  • A+
所属分类:PHP PHP

PHP生成微信小程序码图片,写了一个简单的例子,供大家参考

1.在微信小程序里面配置业务域名,如图:

php生成微信小程序二维码

2.调用微信小程序的接口,使用get传参获取access_token,access_token 的有效期目前为2个小时,可以将其保存到起来,设置session过期时间,保存到sesseion,如图:
php生成微信小程序二维码

3.调用微信小程序二维码接口,使用post传参获取二维码,返回的二维码是二进制数据,可以用file_put_contents转为图片,最后生成了一个test.png的图片,如图:
php生成微信小程序二维码

附上代码:

function test(){
    	$appid = 'wxac3977471957b98e';
    	$secret = 'cea33650e39f449be227a1b4ee244339';
    	$filename = './uploads/test.png';
    	$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        //开启session
        session_start(); 
        // 保存2小时 
        $lifeTime = 2 * 3600; 
        setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
        // echo $url;
        $access_token = $_SESSION['access_token'];
        if(empty($access_token)){
            $access_token_data = $this->getJson($url);
            $access_token = $access_token_data['access_token'];
            $_SESSION['access_token'] = $access_token;
        }
        if(!empty($access_token)){
            $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
            $data['path'] = 'pages/index/index';
            $data['scene'] = 'jobId=222';//(string类型,必须是数字)
            $data['width'] = 430;
            $result = $this->curlPost($url,$data,'POST');
            // p($result);
            $ret = file_put_contents('./uploads/test.png', $result, true);
            echo '成功';
        }else{
            echo 'string';
        }
 }
function getJson($url,$data=array(),$method='GET'){
        $ch = curl_init();//1.初始化  
        curl_setopt($ch, CURLOPT_URL, $url);//2.请求地址  
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
        //4.参数如下  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        if($method=="POST"){//5.post方式的时候添加数据  
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
        }  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
}

function curlPost($url,$data,$method){  
        $ch = curl_init();   //1.初始化  
        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
        //4.参数如下  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器  
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
            curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
          
        if($method=="POST"){//5.post方式的时候添加数据  
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
        }  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $tmpInfo = curl_exec($ch);//6.执行  
      
        if (curl_errno($ch)) {//7.如果出错  
            return curl_error($ch);  
        }  
        curl_close($ch);//8.关闭  
        return $tmpInfo;  
}  

来自时刻需

 

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: