PHP实现生成小程序二维码带参数进入指定页面、小程序URL scheme实现携带数据跳转小程序

简介: PHP实现生成小程序二维码带参数进入指定页面、小程序URL scheme实现携带数据跳转小程序

图片二维码生成

<?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);
}


相关文章
|
25天前
|
缓存 小程序 UED
微信小程序如何在切换页面后原页面状态不变
微信小程序如何在切换页面后原页面状态不变
24 0
|
1月前
|
JSON 小程序 JavaScript
【微信小程序】-- 自定义组件 - 组件所在页面的生命周期 & 插槽(三十七)
【微信小程序】-- 自定义组件 - 组件所在页面的生命周期 & 插槽(三十七)
|
2月前
|
存储 缓存 小程序
小程序页面路由传参的方法?
小程序页面路由传参的方法?
|
1月前
|
小程序
【微信小程序】-- 页面导航 -- 编程式导航(二十三)
【微信小程序】-- 页面导航 -- 编程式导航(二十三)
|
7天前
|
XML JSON API
快速淘宝商品详情页面API接口传输 php
PI(Application Programming Interface,应用程序接口)是一组预定义的函数、协议和工具,用于构建软件应用程序之间的交互。它允许不同的软件系统和应用通过统一的接口进行数据交换和通信
|
25天前
|
缓存 小程序 开发者
微信小程序如何刷新当前页面
微信小程序如何刷新当前页面
16 0
|
1月前
|
运维 Linux Apache
LAMP架构调优(十)——Apache禁止指定目录PHP解析与错误页面优化
LAMP架构调优(十)——Apache禁止指定目录PHP解析与错误页面优化
199 2
|
1月前
|
PHP 数据安全/隐私保护
PHP页面如何实现设置独立访问密码
PHP网页如果需要查看信息必须输入密码,验证后才可显示出内容的代码如何实现? 对某些php页面设置单独的访问密码,如果密码不正确则无法查看内容,相当于对页面进行了一个加密
12 2
PHP页面如何实现设置独立访问密码
|
1月前
|
存储 JSON 小程序
【微信小程序】-- 页面处理总结(三十一)
【微信小程序】-- 页面处理总结(三十一)
|
1月前
|
JSON 小程序 API
【微信小程序】-- 案例 - 本地生活(列表页面)(三十)
【微信小程序】-- 案例 - 本地生活(列表页面)(三十)