码迷,mamicode.com
首页 > 微信 > 详细

PHP生成小程序二维码

时间:2019-09-02 15:40:30      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:create   filename   method   array   make   fwrite   curl   ||   ini   

 

 

/**
     * [生成小程序二维码]
     * @return [type] [description]
     */
    public function makeMiniQrcode_do() {

        begin:

        $id = input(‘id‘);// 获取token
        $ACCESS_TOKEN = $this->getAccesstoken();
        // 准备进入小程序的参数
        $param = json_encode(array("path" => "pages/shop/shop?mac=123", "width" => 150));
        // 请求微信生成二维码接口
        $request_url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
        $result = httpRequest($request_url, $param, "POST");
        // 准备文件名
        $filename = date(‘YmdHis‘) . md5(time() . mt_rand(10, 99)) . ‘.png‘;
        $filepath = "/public/img/miniqrcode/" . $filename;
        // 将二进制图片写入文件
        if (@$fp = fopen(ROOT_PATH . $filepath, ‘w+‘)) {
            fwrite($fp, $result);
            fclose($fp);
        }
        // 判断写入图片是否正常 如果不正常 则再次请求
        $info = getimagesize(ROOT_PATH . $filepath);
        if (!$info) {
            unlink(ROOT_PATH . $filepath);
            goto begin;
        }
        // 二维码路径写入数据库
        if (file_exists(ROOT_PATH . $filepath)) {
                pdo_update(array(‘id‘ => $id), ‘equipment‘, array(‘miniqrcode‘ => $filepath));
                // header(‘Content-type:image/png‘);
                // echo file_get_contents(ROOT_PATH . $filepath);
        }
    }

    /**
     * [获取AccessToken]
     * @return [type] [description]
     */
    private function getAccesstoken() {
        header(‘content-type:text/html;charset=utf-8‘);
        //配置APPID、APPSECRET
        $config = config(‘mini_program‘);
        $APPID = $config[‘APPID‘];
        $APPSECRET = $config[‘APPSECRET‘];
        // 请求地址
        $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";

        $ACCESS_TOKEN = "";
        // session中没有token
        if (!isset($_SESSION[‘access_token‘]) || (isset($_SESSION[‘expires_in‘]) && time() > $_SESSION[‘expires_in‘])) {
            $jsonResult = httpRequest($getTokenUrl);
            $jsonResult = json_decode($jsonResult, true);
            // var_dump($json);
            $_SESSION[‘access_token‘] = $jsonResult[‘access_token‘];
            $_SESSION[‘expires_in‘] = time() + 7200;
            $ACCESS_TOKEN = $jsonResult["access_token"];
        } else {
            // session中有token 直接使用
            $ACCESS_TOKEN = $_SESSION["access_token"];
        }
        return $ACCESS_TOKEN;
    }

 

/**
 * curl 请求
 * @param  [type] $url    [请求地址]
 * @param  string $data   [参数]
 * @param  string $method [请求方式]
 * @return [type]         [description]
 */
function httpRequest($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;
}

 

 

 

 

 

 

 

 

 

 

 

 

1

PHP生成小程序二维码

标签:create   filename   method   array   make   fwrite   curl   ||   ini   

原文地址:https://www.cnblogs.com/Skate0rDie/p/11446822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!