PHP生成小程序二维码
Posted skate0rdie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP生成小程序二维码相关的知识,希望对你有一定的参考价值。
/** * [生成小程序二维码] * @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生成小程序二维码的主要内容,如果未能解决你的问题,请参考以下文章