PHP生成动态小程序二维码自定义路径和参数

PHP生成动态小程序二维码自定义路径和参数
小程序路径src 参数params
http://test.com?src=index/index/index&params=store_id=10
<?php
$src=$_GET['src'];
$params=$_GET['params'];

doPageNewQr($src,$params);
function doPageNewQr($src,$params){
    //配置APPID、APPSECRET
    $APPID = 'wxadd';  //换成自己的
    $APPSECRET = 'sdadsa';//换成自己的

    $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
    //缓存access_token
    session_start();
    $_SESSION['access_token'] = "";
    $_SESSION['expires_in'] = 0;


    if(!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in']))
    {

        $json =http( $access_token );
        $json = json_decode($json,true);

        // var_dump($json);
        $_SESSION['access_token'] = $json['access_token'];
        $_SESSION['expires_in'] = time()+7200;
        $ACCESS_TOKEN = $json["access_token"];
    }
    else{

        $ACCESS_TOKEN =  $_SESSION["access_token"];
    }
    $str= str_replace(["&","="],['*','/'],$params);
    $qcode = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN."";
    $param = json_encode(array("scene"=>$str,"page"=>$src,"width"=>100));//与第一个图片生成不同点
    $result = http($qcode, $param,"POST");
    //生成二维码
    $new_file=__DIR__ . "/img";
    if(!file_exists($new_file)){
        //检查是否有该文件夹,如果没有就创建,并给予最高权限
        mkdir($new_file, 0700);
    }
    $img_name=md5($params);
    $file=$new_file . "/img_" . $img_name.".png";
    $res= file_put_contents($file, $result);
    echo $file;
}

function http($url, $data='', $method='GET'){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    if($method=='POST')
    {
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data != '')
        {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    }

    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

 

猜你喜欢

转载自blog.csdn.net/weixin_39934453/article/details/131963949