日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java获取微信短链接_微信公众号实现长链接转短链接!w.url.cn短网址生成

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java获取微信短链接_微信公众号实现长链接转短链接!w.url.cn短网址生成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

微信公眾號開發者平臺提供短網址生成的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

總結

以上是生活随笔為你收集整理的java获取微信短链接_微信公众号实现长链接转短链接!w.url.cn短网址生成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。