• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

微信公众号实现长链接转短链接wurlcn短网址生成

php 搞代码 3年前 (2022-03-01) 15次浏览 已收录 0个评论
文章目录[隐藏]

微信公众号开发者平台提供短网址生成的API,最终生成的短网址是w.url.cn的,上面是具体的代码。

官网文档

https://developers.weixin.qq.com/doc/offiaccount/Account_Management/URL_Shortener.html

申请参数

access_token
action 此处填long2short,代表长链接转短链接
long_url 须要转换的原链接

HTTP POST申请网址

https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN

申请流程

1、获取本地缓存的access_token,如果超过有效期,则从新获取,如果还没过期,间接应用缓存的access_token
2、构建申请参数,发动POST申请
3、取得短网址

代码

appid和appsecret能够申请一个微信公众号测试账号进行开发,申请地址:http://mp.weixin.qq.com/debug…

<?php
header("Content-type:application/json");

// 申明APPID、APPSECRET
$appid = "xxx";
$appsecret = "xxx";

// 获取access_token和jsapi_ticket
function getToken(){
    $file = file_get_contents("access_token.json",true);//读取access_token.json外面的数据
    $result = json_decode($file,true);

//判断access_token是否在有效期内,如果在有效期则获取缓存的access_token
//如果过期了则申请接口生成新的access_token并且缓存access_token.json
if (time() > $result['expires']){
        $data = array();
        $data['access_token'] = getNewToken();
        $data['expires'] = time()+7000;
        $jsonStr =  json_encode($data);
        $fp = fopen("access_token.json", "w");
        fwrite($fp, $jsonStr);
        fclose($fp);
        return $data['access_token'];
    }else{
        return $result['access_token'];
    }
}
 
//获取新的access_token
function getNewToken($appid,$appsecret){
    global $appid;
    global $appsecret;
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
    $access_token_Arr =  file_get_contents($url);
    $token_jsonarr = json_decode($access_token_Arr, true);
    return $token_jsonarr["access_token"];
}

// 取得长链接
$long_url = trim($_GET["long_url"]);

// 过滤
if (empty($long_url)) {
    $result = array(
        "result" => "101",
        "msg" => "请传入长链接"
    );
} else if (strpos($long_url,'http') !== false){
    //初始化 CURL
    $ch = curl_init();
    //申请地址 
    curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token='.getToken());
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    $postdata =  '{"action":"long2short","long_url":"'.$long_url.'"}'; 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    // 对认证证书起源的查看
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    // 从证书中查看SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    //获取的信息以文件流的模式返回,而不是间接输入
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //发动申请
    $dwzStr = curl_exec($ch);
    //解析数据
    $arr_dwzStr = json_decode($dwzStr, true);
    $dwz = $arr_dwzStr["short_url"];
    //敞开申请
    curl_close($ch);

    // 返回后果
    $result = array(
        "result" => "100",
        "msg" => "解析胜利",
        "dwz" => $dwz
    );
}else{
    $result = array(
        "result" => "102",
        "msg" => "长链接不非法"
    );
}

// 返回JSON
echo json_encode($result,JSON_UNESCAPED_UNICODE);
?>

生成示例

{"result":"100","msg":"生成胜利","dwz":"https:\/\/w.url.cn\/s\/AF5p0UM"}

体验

http://www.likeyunba.com/api/wurl/?long_url=长链接

Author:TANKING
Date:2020-09-18
Web:http://www.likeyun.cn
WeChat:face6009


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:微信公众号实现长链接转短链接wurlcn短网址生成
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址