微信小程序二维码获取和保存 PHP

微信小程序二维码获取和保存 PHP

<?php 

class Wx {
     /* 请求地址 */
    protected $url;
    /* 微信配置 */
    protected $config;

    public function __construct(){
        $this->url = 'https://api.weixin.qq.com/';
        $this->config = array(
            'appid' => '',
            'mch_id' => '',
            'AppSecret' => '',
            'key' => '',
            'notify_url' => ''
        );
    }

    /**
     * 获取微信token
     * @return boolean|mixed array('access_token'=>'ACCESS_TOKEN','expires_in'=>'有效时间','death_line'=>'失效时间戳')
     */
    public function get_access_token(){
        $token_cache = F('wx_token');
        /* 判断缓存token是否失效,未失效是直接使用缓存的的token,已失效则重新获取 */
        if($token_cache['death_line']<=time()){
            $ext_url = 'cgi-bin/token';
            $grant_type = 'client_credential';
            $appid = $this->config['appid'];
            $secret = $this->config['AppSecret'];
            $param = array(
                'grant_type'=>$grant_type,
                'appid'=>$appid,
                'secret'=>$secret,
            );
            $full_url = $this->build_full_url($this->url.$ext_url, $param);
            $result = $this->curl_get($full_url);
            if($result!==FALSE){
                $result = json_decode($result,true);
                /* 设置失效时间 */
                $result['death_line'] = time() + $result['expires_in'];
                F('wx_token',$result);
            }
        }else{
            $result = F('wx_token');
        }
        return $result;
    }

    /**
     * 获取小程序二维码
     * @param array $data 需要传递的参数,微信限制(最大32个可见字符,键名+键值+连接符['='|'&']<=32)
     * @param string $page 已经发布的小程序存在的页面,根路径前不要填加'/',不能携带参数(参数请放在scene字段里)
     * @param number $width 二维码的宽度 
     * @param boolean $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
     * @param array $line_color auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示
     * @param boolean $is_hyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码
     * @return boolean|mixed
     */
    public function get_mini_code($data,$page,$width=430,$auto_color=false,$line_color=array('r'=>'0','g'=>'0','b'=>'0'),$is_hyaline=false){
        $token_info = $this->get_access_token();
        $ext_url = 'wxa/getwxacodeunlimit';
        $full_url = $this->build_full_url($this->url.$ext_url, array('access_token'=>$token_info['access_token']));
        $wx_data = array(
            'scene'=>'?'.http_build_query($data),
            'page'=>$page,
            'width'=>$width,
            'auto_color'=>$auto_color,
            'line_color'=>$line_color,
            'is_hyaline'=>$is_hyaline,
        );
        $wx_data = json_encode($wx_data);
        $res = $this->curl_post($full_url,$wx_data);
        return $res;
    }

    /**
     * 
     * @param string $url
     * @param string $param
     * @return string
     */
    public function build_full_url($url,$param){
        $param_str = http_build_query($param);
        /* 判断原链接是否已有参数 */
        $joiner = strpos($string,'?')===false ? '?' : '&';
        /* 拼接url */
        $full_url = $url.$joiner.$param_str;
        return $full_url;
    }

    /**
     * 模拟GET请求
     */
    public function curl_get($url){
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'limuzhengxin.com' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt( $ch , CURLOPT_URL , $url);
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

    /**
     * 模拟POST请求
     */
    public function curl_post($url,$params=false){
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'limuzhengxin.com' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt( $ch , CURLOPT_POST , 1 );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }


}
/**
 * 获取小程序二维码并保存到文件夹
 * @param string $user_id 用户ID
 * @return string 小程序二维码保存路径
 * @author lidong<947714443@qq.com>
 */
function get_mini_code_path($user_id){
    /* 小程序二维码保存目录 */
    $file_path = 'public/upload/user/mini_qrcode/';
    mkdir_chmod($file_path);
    $filename = $file_path.$user_id.'.png';
    if(!file_exists($filename)){
        $Wx = new Wx();
        $data=array(
            'id'=>$goods_id,
        );
        $page = ''; /* 小程序详情页 */
        $res = $Wx->get_mini_code($data, $page);
        $im = file_put_contents($filename, $res);
    }
    $img = getHttpType().$_SERVER['HTTP_HOST'].'/'.$filename;
    return $img;
}

/**
 * 文件夹创建并且给权限
 * @param string $path 文件夹路径
 * @param string $mode 权限,默认'0777'
 * @return bool 
 * @author lidong<947714443@qq.com>
 */
function mkdir_chmod($path, $mode = 0777){
    if(is_dir($path)) {
        return true;
    }
    $result = mkdir($path, $mode, true);
    if($result) {
        $path_arr = explode('/',$path);
        $path_str = '';
        foreach($path_arr as $val){
            $path_str .= $val.'/';
            $a = chmod($path_str,$mode);
        }
    }
    return $result;
}

/**
 * 获取http协议类型 (http:// || https://)
 * 
 * @return string(http:// || https://)
 * @author lidong<947714443@qq.com>
 */
function getHttpType(){
    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
    return $http_type;
}

代码扣出来的,可能没法直接跑,不过大概意思应该都能明白;
代码原来是在TP3.2里的 因为涉及到一些客户隐私,所以把相关的信息去掉了,将就着看下吧
附:小程序二维码文档

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laydown__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值