小程序ShortLink URLScheme

<?php
namespace app\controller;

use app\BaseController;
use think\Controller;
use think\Response;
use think\Config;
use think\Db;//全局变量应用
class Home extends BaseController {
    public function generateShortLink(){
        $page_url = $this->request->get('page_url');
        $appid = $this->request->get('appid');
        $secret = $this->request->get('secret');
        $access_token = get_token($appid, $secret);
        $ma = $this->get_curl("https://api.weixin.qq.com/wxa/genwxashortlink?access_token=".$access_token,json_encode(['page_url'=>$page_url]));
        Db::execute("insert into `url_ok` (appid,secret,url_link,times) values ('".$appid."','". $secret ."','". json_decode($ma, true)['url_link'] ."','". date("Y-m-d") ."')");
        return json(json_decode($ma, true));//{"errcode":0,"errmsg":"ok","link":"#小程序:\/\/香水助手\/vJwTQNtSItsahfk"}
    }
    public function generateScheme(){
        $page_url = $this->request->get('page_url');
        $appid = $this->request->get('appid');
        $secret = $this->request->get('secret');
        $access_token = get_token($appid, $secret);
        $ma = $this->get_curl("https://api.weixin.qq.com/wxa/generatescheme?access_token=".$access_token,
        json_encode([
            "jump_wxa" => [
                "path" => $page_url,
                "query" => "",
                "env_version" => "release"
            ],
            "is_expire" => true,
            "expire_type" => 1,
            "expire_interval" => 1
        ]));
        Db::execute("insert into `url_ok` (appid,secret,url_link,times) values ('".$appid."','". $secret ."','". json_decode($ma, true)['url_link'] ."','". date("Y-m-d") ."')");
        return json(json_decode($ma, true));//{"errcode":0,"errmsg":"ok","openlink":"weixin:\/\/dl\/business\/?t=Yl04MVDjtrh"}
    }
    public function generateUrlLink(){
        date_default_timezone_set ('Asia/Shanghai');//上海时间标准
        $page_url = $this->request->get('page_url');
        $appid = $this->request->get('appid');
        $secret = $this->request->get('secret');
        $access_token = get_token($appid, $secret);
        $ma = $this->get_curl("https://api.weixin.qq.com/wxa/generate_urllink?access_token=".$access_token,
        json_encode([
            "path" => $page_url,
            "query" => "",
            "expire_type" => 1,
            "expire_interval" => 30
        ]));
        Db::execute("insert into `url_ok` (appid,secret,url_link,times) values ('".$appid."','". $secret ."','". json_decode($ma, true)['url_link'] ."','". date("Y-m-d") ."')");
        return json(json_decode($ma, true));//{"errcode":0,"errmsg":"ok","url_link":"https://wxaurl.cn/zHaPufB5SBf"}
    }





    public function hello() {
        return 'hello,';
    }
    private function get_token($appid, $secret) {
        $ma = $this->get_curl("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret);
        $json = json_decode($ma, true);
        if (array_key_exists('errcode', $json)) {
            echo $ma;
            return $ma;// 如果错误就直接把微信的错误返回
        }
        return $json['access_token'];
    }
    private function get_curl($url, $post=0, $referer=0, $cookie=0, $header=0, $ua=0, $nobaody=0) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $httpheader[] = "Accept:application/json";
        $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
        $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
        $httpheader[] = "Connection:close";
        $httpheader[] = "Content-Type: application/json";  // 添加Content-Type头
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);

        if ($post) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        if ($header) {
            curl_setopt($ch, CURLOPT_HEADER, true);
        }
        if ($cookie) {
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        }
        if ($referer) {
            if ($referer == 1) {
                curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
            } else {
                curl_setopt($ch, CURLOPT_REFERER, $referer);
            }
        }
        if ($ua) {
            curl_setopt($ch, CURLOPT_USERAGENT, $ua);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0");
        }
        if ($nobaody) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容