
因为前段时间新浪关闭了短网址生成服务,导致我刚刚才申请没两天的应用就用不了了,写的接口也没用了(嘤嘤嘤),所以前两天在网上找短网址生成的相关文章时无意间看到了关于短网址还原为长网址的文章,就试着弄了一下。
下面放代码
Url.Class.php
<?php
/***
* 短网址还原函数
* @param $shortUrl 短网址
* @return 原始网址 | 空(还原失败或非短网址)
*/
function restoreUrl($shortUrl) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $shortUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0');
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
$data = curl_exec($curl);
$curlInfo = curl_getinfo($curl);
curl_close($curl);
if($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) {
return $curlInfo['redirect_url'];
}
return '';
}
?>
关于调用
举个栗子:Url.php
<?php
header('Access-Control-Allow-Origin:*');
header('Content-type: application/json');
require_once('Url.Class.php');
$url = $_GET['url'];
$shortUrl = $url; // 要进行还原的短网址
$orinalUrl = restoreUrl($shortUrl);
if($orinalUrl) {
echo "{\"Code\":\"200\",\"Msg\":\"Success\",\"ShortUrl\":\"$shortUrl\",\"LongUrl\":\"$orinalUrl\"}";
} else {
echo "{\"Code\":\"400\",\"Msg\":\"Failure\",\"Tips\":\"Bad URL Parameter.\"}";
}
?>
例如https://url.cn/5TZtMKp、http://t.cn/AiDxePyW、http://suo.im/5OniK4、http://suo.im/6is6OW之类的利用301或者302跳转的短网址都可以进行还原。
请求成功接口返回示例
{
"Code":"200",
"Msg":"Success",
"ShortUrl":"http://suo.im/6is6OW",
"LongUrl":"https://blog.badapple.pro/"
}
请求失败接口返回示例
{
"Code":"400",
"Msg":"Failure",
"Tips":"Bad URL Parameter."
}
以上
Comments | 5 条评论
诶?新浪短网址其实还可以用的叭…?只是需要多点击一下了…
嘤嘤嘤~ 呐呐呐~
@蝉時雨 @蝉時雨:( •̆ ᵕ •̆ )◞♡
看我又发现了啥有趣的东西[smilies35][smilies19]
@Xiaomo @Xiaomo:嘤嘤嘤[smilies11]