微信获取带参数二维码

index.php


function loader($class)
{
$file=$class.".php";
include_once($file);
}
spl_autoload_register('loader');

if(!empty($_GET['qr_url'])){
$down= new Down();
$state=$down->downQrcode($_GET['qr_url']);
if($state){
print_r("保存成功!");
}else{
print_r("保存失败!");
}
exit;
}
$getdata = new GetData();
$scenes = array(1,2,3,4,5,6);
$qrcode_urls=array();
$qrcode_urls=$getdata->getToken('wxf84e0aba5d2f90fa','f8266b993011355f6da2f4dc79eed148',$scenes);
if(!empty($qrcode_urls['error'])){
echo $qrcode_urls['error'];
}else{
foreach($qrcode_urls as $key=>$value){
print_r("$valuedown");
}
}


Down.php

class Down
{
public function downQrCode($qr_url){
if(empty(trim($qr_url))){
return false;
}
if (!file_exists("./Qrcode") && !mkdir("./Qrcode", 0777, true)) {
return false;
}
$filename=time().".jpg";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $qr_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
try{
$img = curl_exec($ch);
$imgtype= curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if($imgtype=="image/jpg"){
$start=file_put_contents("./Qrcode/".$filename, $img);
}else{
$start= false;
}
}catch(Exception $e){
echo $e->getMessage();
return false;
}
if(!$start){
return false;
}
unset($img, $url);
return true;
}
}

Util.php

class Util{
public function jsonCurl($url, $json_data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if(!empty($json_data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
try{
$result = curl_exec($curl);
}catch(Exception $e){
echo $e->getMessage();
}
return $result;
}
}


getData.php

class GetData{
public function getToken($appid,$secret,$scenes){
$util = new Util();
if(empty($appid)||empty($secret)){
echo 'error!!!!!!';
return array("error"=>"appid或secret为空!");
}
$access_token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
try{
$content=$util->jsonCurl($access_token_url,$json_data="");
$content=json_decode($content,true);
}catch(Exception $e){
echo $e->getMessage();
}
if(empty($content['access_token'])){
return array("error"=>"获取access_token失败!");
}
$qrcode_urls=array();
foreach($scenes as $key=>$value){
$ticket=$this->getTicket($content['access_token'],$value);
if($ticket!="error"){
$qrcode_urls[$key]="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket;
}else{
$qrcode_urls[$key]="二维码生成失败!";
}
}
return $qrcode_urls;
}

private function getTicket($access_token,$scenes,$qrcode_permanent=true){
$util=new Util();
$data=array();
if($qrcode_permanent){
        $data['action_info']['scene']['scene_str']=$scenes;
        $data['action_name']="QR_LIMIT_STR_SCENE";
    }else{
        $data['expire_seconds']=604800;
        $data['action_info']['scene']['scene_id']=$scenes;
        $data['action_name']="QR_SCENE";
    }
    $ticket_url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
    try{    
        $data=json_encode($data);
        $ticket=$util->jsonCurl($ticket_url,$data);
        if(empty($ticket)){
            return  "error";
        }
        $ticket=json_decode($ticket,true);
        if(empty($ticket['ticket'])){
            return "error";
        }
    }catch(Exception $e){
        throw new Exception("getTicket error!");
        return "error";
    }
    return $ticket['ticket'];
}

}



你可能感兴趣的:(微信获取带参数二维码)