微信开发笔记:生成带参数的公众号二维码

 扫一扫进公众号,其实是可以提交参数的。一个公众号可以提供无线多个临时带参数二维码和有限多个永久的带参数二维码,我们就拿永久的带参二维码为例,毕竟这个比较常用一些。操作流程也很常规获取access_token->换取ticket ->换取带参二维码:

<?php
define("APPID", APPID);
define("APPSECRET", APPSECRET);

class wx_user_qr_code
{
    function __construct(){
        $this->wx_user_qr_code();
    }
    function wx_user_qr_code(){
    }
    function get_access_token(){
    $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
    $res = file_get_contents($token_access_url);
    $result = json_decode($res, true); 
    $access_token = $result[‘access_token‘];
    return $access_token;
}
function generate_qrcode($scene_id){
    $access_token = $this->get_access_token();
    $url = ‘https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=‘.$access_token;
    $parameters = array(
        "action_name"=>"QR_LIMIT_SCENE",
        "action_info"=>array("scene"=>array("scene_id"=>$scene_id))
    );
    $result = $this->postCurl(json_encode($parameters, true),$url);
    $return_data = json_decode($result,true);

    $ticket = $return_data["ticket"];
    $img_url = $return_data["url"];
    $codeurl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
        
    $img = file_get_contents($codeurl);
    $file_name = "scene_id_".$scene_id."_".time().".jpg";
    file_put_contents(‘./user_affiliate_qr/‘.$file_name,$img);
    return $file_name;
}
function postCurl($send_data, $url, $second = 30){
        // 初始化curl
        $ch = curl_init();
        // 设置超时
        curl_setopt($ch, CURLOP_TIMEOUT, $second);
        // 这里设置代理,如果有的话
        // curl_setopt($ch,CURLOPT_PROXY, ‘8.8.8.8‘);
        // curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // 设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        // post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
        // 运行curl
        
        $data = curl_exec($ch);
        curl_close($ch);
        // 返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "curl出错,错误码:$error" . "<br>";
            echo "<a href=‘http://curl.haxx.se/libcurl/c/libcurl-errors.html‘>错误原因查询</a></br>";
            curl_close($ch);
            return false;
        }
    }    
}
?>    
    

 

微信开发笔记:生成带参数的公众号二维码

上一篇:开通了个人微信公众号:slbGTD,准备把GTD相关的内容写成一本书


下一篇:微信 ios的问题