图片二维码生成
<?php //二进制转图片image/png function burryToBase64($contents, $mime){ $base64 = base64_encode($contents); return ('data:' . $mime . ';base64,' . $base64); } function getAccessToken(){ $grant_type = 'client_credential'; $appid = 'wx56dde2c282ad225b'; $secret = 'ff76cf2da3af30fb6fcb421016ffd67e'; $url='https://api.weixin.qq.com/cgi-bin/token?grant_type='.$grant_type.'&appid='.$appid.'&secret='.$secret; $accessToken = curlGet($url); return $accessToken['access_token']; } function curlGet($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $output = curl_exec($ch); curl_close($ch); return json_decode($output,true); } header("Content-Type: text/html; charset=utf-8"); function index($code){ //code为参数 // echo $url; $access_token = getAccessToken(); if(!empty($access_token)){ $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token; $data['path'] = "/pages/index/index?url=https://dl.99tv.top/".$code; $data['scene'] = 'type=qrcode';//(string类型,必须是数字) $data['width'] = 430; $result = curlPost($url,$data,'POST'); $image = burryToBase64($result,'image/png'); $filename = md5($code); //文件名 $ret = file_put_contents('./uploads/'.$filename.'.png', $result, true); $path='./uploads/'.$filename.'.png'; return $image; }else{ echo '获取asstoken失败'; } } function getJson($url,$data=array(),$method='GET'){ $ch = curl_init();//1.初始化 curl_setopt($ch, CURLOPT_URL, $url);//2.请求地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式 //4.参数如下 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); if($method=="POST"){//5.post方式的时候添加数据 $data = json_encode($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } function curlPost($url,$data,$method){ $ch = curl_init(); //1.初始化 curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式 //4.参数如下 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容 curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); if($method=="POST"){//5.post方式的时候添加数据 $data = json_encode($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch);//6.执行 if (curl_errno($ch)) {//7.如果出错 return curl_error($ch); } curl_close($ch);//8.关闭 return $tmpInfo; } $result=index('zt'); echo "<img src='$result'>" ?>
链接跳转
前端
<a href="<?php echo jump($value['tinyurl']);?>" style="color:white;font-size:20px;">打开小程序</a>
调用代码
<?php header('Content-type: text/html; charset=utf-8'); //引用加密文件 //入口文件 //jump($id); /** * 跳转小程序 */ function curlGet($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $output = curl_exec($ch); curl_close($ch); return json_decode($output,true); } function getAccessToken(){ $grant_type = 'client_credential'; $appid = 'wx56dde2c282ad225b'; $secret = 'ff76cf2da3af30fb6fcb421016ffd67e'; $url='https://api.weixin.qq.com/cgi-bin/token?grant_type='.$grant_type.'&appid='.$appid.'&secret='.$secret; $accessToken = curlGet($url); return $accessToken['access_token']; } function jump($id) { $access_token = getAccessToken(); $url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token=' . $access_token . ''; $jump_wxa = [ 'path' => '/pages/index/index', 'query' => "url=https://dl.99tv.top/$id", 'env_version' => 'trial' ]; $params = [ 'jump_wxa' => (object)$jump_wxa, 'expire_type' => 0, 'expire_time' => time() + 1000 ]; $res = curl_post($url, $params); $res = json_decode($res); // var_dump($res); if ($res->errcode == 0) { return $res->openlink; // echo json_encode(['url' => $res->openlink, 'code' => 1]); } } function curl_get($url){ $header = array( 'Accept: application/json', 'isOpenSecret: 1' ); $curl = curl_init(); //设置抓取的url curl_setopt($curl, CURLOPT_URL, $url); //设置头文件的信息作为数据流输出 curl_setopt($curl, CURLOPT_HEADER, 0); // 超时设置,以秒为单位 curl_setopt($curl, CURLOPT_TIMEOUT, 1); // 超时设置,以毫秒为单位 // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500); // 设置请求头 curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //设置获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //执行命令 $data = curl_exec($curl); // 显示错误信息 if (curl_error($curl)) { print "Error: " . curl_error($curl); } else { // 打印返回的内容 // print_r($data); return $data; curl_close($curl); } } /** * 获取accessToken */ /** * @param string $url post请求地址 * @param array $params * @return mixed */ function curl_post($url, array $params = array()) { $data_string = json_encode($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); $data = curl_exec($ch); curl_close($ch); return ($data); }