微信获取带参二维码

#

/**
 * 二维码生成图片生成
 * type 0临时 1永久
 */

public function get_img_url($scene_id,$scene_str,$type=0,$expire_seconds=30)
{
    $token ='';
    $access_token = '';

    switch ($type) {
        case 0:
            return $this->get_temp_qr_url($access_token, $expire_seconds, $scene_id,$scene_str);
            break;
        case 1:
            return $this->get_qr_url($access_token,$scene_id,$scene_str);
            break;
    }
}

/**
 * 获取临时
   http请求方式: POST
 * URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
 * POST数据格式:json
 * POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
 * 或者也可以使用以下POST数据创建字符串形式的二维码参数:
 * {"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
*/
public function get_temp_qr_url($access_token, $expire_seconds, $scene_id, $scene_str,$scene_type=1)
{
    $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;

    // 参数为数字
    if ($scene_type == 1) {
        $data = [
            'expire_seconds' => $expire_seconds,
            'action_name'    => 'QR_SCENE',
            'action_info'    => [
                'scene'=>[
                    'scene_id'=>$scene_id,
                ]
            ],
        ];
    }
    // 参数为字符串
    else{   
        $data = [
            'expire_seconds' => $expire_seconds,
            'action_name'    => 'QR_STR_SCENE',
            'action_info'    => ['scene'=>['scene_str'=>$scene_str]],
        ];        
    }
    $stram = $this->curl_post($url, json_encode($data));

    $stramData = json_decode($stram);
    // $url = $stramData->url;

    $ticket= $stramData->ticket;

    $img_url = $this->get_img_by_ticket($ticket);

    return $img_url;
}

/**
 * 获取永久
 * http请求方式: POST
 * URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
 * POST数据格式:json
 * POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}

 * 或者也可以使用以下POST数据创建字符串形式的二维码参数:
 * {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
 */
public function get_qr_url($access_token, $scene_id, $scene_str,$scene_type=1)
{
    $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;

    // 参数为数字
    if ($scene_type == 1) {
        $data = [
            'action_name'    => 'QR_LIMIT_SCENE',
            'action_info'    => ['scene'=>['scene_id'=>$scene_id]],
        ];
    }
    // 参数为字符串
    else{   
        $data = [
            'action_name'    => 'QR_LIMIT_STR_SCENE',
            'action_info'    => ['scene'=>['scene_id'=>$scene_str]],
        ];        
    }

    $stram = $this->curl_post($url, json_encode($data));

    $stramData = json_decode($stram);

    // $url = $stramData->url;

    $ticket = $stramData->ticket;

    $img_url = $this->get_img_by_ticket($ticket);

    return $img_url;        
}

// 获取资源 写入文件
public function get_img_by_ticket($ticket)
{
    $url  = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);

    $data = $this->curl_get_https($url);

    $path = '/Uploads/Picture/模块名/' . date('Y-m-d') . '/';
    @chmod(SITE_PATH . $path, 0755);//修改权限
    if (!file_exists(SITE_PATH . $path)) {
        mkdir(SITE_PATH . $path, 0755, true);
    }
    //设置权限
    $type= 'png';
    $new_file = md5(uniqid()) . '.' . $type;//用微秒和会员id产生md5唯一字符串
    if (file_put_contents(SITE_PATH . $path . $new_file, $data)) {
        return $path . $new_file;
    } else {
        return false;
    }
}
/**
 * curl->get->post 
 */
function curl_post($url, array $params = array(), $timeout=5)
{
    $ch = curl_init();//初始化
    curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return ($data);
}
// get
function curl_get($url, array $params = array(), $timeout = 5)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    return $file_contents;
}
// get https
function curl_get_https($url)
{
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    //重要!
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"); //模拟浏览器代理

    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    return $data;
}



导航