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


相关文章
|
9月前
|
存储 安全 小程序
在微信小程序中使用 Vant 时如何确保数据的安全?
在微信小程序中使用 Vant 时如何确保数据的安全?
105 1
|
4月前
|
缓存 小程序 API
微信小程序页面导航与路由:实现多页面跳转与数据传递
本文深入探讨微信小程序的页面导航与路由机制,介绍多种页面跳转方式如`wx.navigateTo`、`wx.redirectTo`、`wx.switchTab`等,并讲解通过URL、全局变量和事件传递数据的方法。结合案例实现多页面跳转与数据传递,帮助开发者掌握这一重要技能。
|
4月前
|
缓存 小程序 API
微信小程序网络请求与API调用:实现数据交互
本文深入探讨了微信小程序的网络请求与API调用,涵盖`wx.request`的基本用法、常见场景(如获取数据、提交表单、上传和下载文件)及注意事项(如域名配置、HTTPS协议、超时设置和并发限制)。通过一个简单案例,演示了如何实现小程序与服务器的数据交互。掌握这些技能将帮助你构建功能更丰富的应用。
|
8月前
|
小程序 JavaScript 开发工具
微信小程序——全局数据共享
【10月更文挑战第5天】
|
9月前
|
JSON 小程序 前端开发
微信小程序的目录结构及页面结构的说明
本文详细介绍了微信小程序的目录结构、页面组成部分以及项目的全局配置文件,阐述了小程序的宿主环境和运行机制,包括小程序启动和页面渲染的过程。
微信小程序的目录结构及页面结构的说明
|
8月前
|
小程序 前端开发 开发者
小程序的页面如何布局?
【10月更文挑战第16天】小程序的页面如何布局?
490 1
|
8月前
|
缓存 小程序 UED
如何利用小程序的生命周期函数实现数据的加载和更新?
如何利用小程序的生命周期函数实现数据的加载和更新?
218 4
|
8月前
|
移动开发 小程序 数据可视化
微信小程序可视化开发工具之动态数据
微信小程序可视化开发工具之动态数据
133 3
|
9月前
|
小程序 JavaScript API
微信小程序开发学习之页面导航(声明式导航和编程式导航)
这篇文章介绍了微信小程序中页面导航的两种方式:声明式导航和编程式导航,包括如何导航到tabBar页面、非tabBar页面、后退导航,以及如何在导航过程中传递参数和获取传递的参数。
微信小程序开发学习之页面导航(声明式导航和编程式导航)
|
9月前
|
小程序 开发者
微信小程序之网络数据请求 wx:request的简单使用
这篇文章介绍了微信小程序中如何使用wx.request进行网络数据请求,包括请求的配置、请求的格式以及如何在开发阶段关闭请求的合法检验。
微信小程序之网络数据请求 wx:request的简单使用

热门文章

最新文章