手机浏览器链接跳转微信小程序代码

微信 文章 2021-01-07 17:29 6136 0 全屏看文

AI助手支持GPT4.0

之前想在朋友的手机wap流量引流到我的小程序。然后就让另一个朋友帮我写了这个。从手机浏览器H5页面跳到微信小程序的方法。

<?php


function getAccessToken(){
    $appid = 'APPID';
    $secret = '密钥';
    
    $data = file_get_contents('./token.json');
    $token_data = [];
    if(!empty($data)){
        $token_data = json_decode($data,true);
    }
    if(empty($token_data['time']) || $token_data['time'] < time()){
        
        $res = curl_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret);
        
        
        
        $res_data = json_decode($res,true);
        
        $new_res = [
            'access_token'=>$res_data['access_token'],
            'time'=>time()+7000
        ];
        file_put_contents('./token.json',json_encode($new_res));
        return $res_data['access_token'];
    }else{
        
        return $token_data['access_token'];
        
    }
    
}

/* 部分代码隐藏,需要的请加我微信 shasanshu 150元奉上!*/
function curl_get($url) {
    // 创建一个新cURL资源
    $ch = curl_init();
  
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 跳过证书验证(https)的网站无法跳过,会报错
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书验证
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回的数据不打印
    // 抓取URL并把它传递给浏览器
    $rs = curl_exec($ch);
    if($rs === false) {
        return 'Curl error: ' . curl_error($ch);
    }
    // 关闭cURL资源,并且释放系统资源
    curl_close($ch);
    return $rs;
}

function http_request( $url, $post = '', $timeout = 5 ){ 
    if( empty( $url ) ){
     return ;
    }
    $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_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
     
    if( $post != '' && !empty( $post ) ){
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($post)));
    }
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

echo getUrlscheme();


测试链接:

https://www.sanshu.cn/test/wxlite1/index.php

测试二维码

image.png

-EOF-

AI助手支持GPT4.0