生成带参数的小程序二维码

php php 小程序 982      收藏
生成带参数的小程序二维码

/****
 * 生成带参数的小程序二维码--接口B
 * @param string $access_token 通过微信接口获取的AccessToken
 * @param string $scene 场景码
 * @param bool $auto_color 是否使用默认颜色
 * @param array $line_color 颜色的rbg数据
 * @param string $width 二维码宽度
 * @param string $page 要跳转的页面
 * @return array|string 错误信息|图片路径
 */
function MiniWxCreateQrCode($access_token,$scene,$auto_color = false ,$line_color = ["r"=>0,"g"=>0,"b"=>0] ,$width="430",$page = ""){
    $apiurl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
    $request_data = [
        'scene'=>$scene,
        'page'=>$page,
        'width'=>$width,
        'auto_color'=>$auto_color,
        'line_color'=>$line_color,
    ];
    //保存的路径
    $save_qr_path = ROOT_PATH.config('wx_qr_path')."/";
    $save_qr_path = str_replace("\\","/",$save_qr_path); 
    //成功的话返回图片的二进制数据--失败的话返回json字符串
    $output = go_curl($apiurl,"post",$request_data,"json");

    if(substr($output , 0 , 1) == "{"){
        return json_decode($output,true);
    }
    $http_qr_path = get_host().config('wx_qr_path')."/";
    $save_name = $scene."_".date('YmdHis').".jpg";
    file_put_contents($save_qr_path.$save_name,$output);
    return $http_qr_path.$save_name;
}


/**
 * curl访问
 * @author rainfer <81818832@qq.com>
 * @param  string $url
 * @param string $type
 * @param boolean $data
 * @param string $err_msg
 * @param int $timeout
 * @param array $cert_info
 * @return string
 */
function go_curl($url, $type, $data = false, $dataType ="", &$err_msg = null, $timeout = 20, $cert_info = array())
{
	$type = strtoupper($type);
    if ($type == 'GET' && is_array($data)) {
        $data = http_build_query($data);
    }
    $option = array();
    if ( $type == 'POST' ) {
        $option[CURLOPT_POST] = 1;
    }
    if($dataType == "json"){
        $data = json_encode($data);
    }
    if ($data) {
        if ($type == 'POST') {
            $option[CURLOPT_POSTFIELDS] = $data;
        } elseif ($type == 'GET') {
            $url = strpos($url, '?') !== false ? $url.'&'.$data :  $url.'?'.$data;
        }
    }
    $option[CURLOPT_URL]            = $url;
    $option[CURLOPT_FOLLOWLOCATION] = TRUE;
    $option[CURLOPT_MAXREDIRS]      = 4;
    $option[CURLOPT_RETURNTRANSFER] = TRUE;
    $option[CURLOPT_TIMEOUT]        = $timeout;
    //设置证书信息
    if(!empty($cert_info) && !empty($cert_info['cert_file'])) {
        $option[CURLOPT_SSLCERT]       = $cert_info['cert_file'];
        $option[CURLOPT_SSLCERTPASSWD] = $cert_info['cert_pass'];
        $option[CURLOPT_SSLCERTTYPE]   = $cert_info['cert_type'];
    }
    //设置CA
    if(!empty($cert_info['ca_file'])) {
        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
        $option[CURLOPT_SSL_VERIFYPEER] = 1;
        $option[CURLOPT_CAINFO] = $cert_info['ca_file'];
    } else {
        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
        $option[CURLOPT_SSL_VERIFYPEER] = 0;
    }
    $ch = curl_init();
    curl_setopt_array($ch, $option);
    $response = curl_exec($ch);
    $curl_no  = curl_errno($ch);
    $curl_err = curl_error($ch);
    curl_close($ch);
    // error_log
    if($curl_no > 0) {
        if($err_msg !== null) {
            $err_msg = '('.$curl_no.')'.$curl_err;
        }
    }
    return $response;
}