hoo*_*ter 28 twitter url-shortener
给定t.co链接,我如何找到链接解析的位置?例如,如果我有t.co/foo,我想要一个返回domain.com/bar的函数或进程.
Jon*_*ill 19
我会远离你无法控制的外部API.这只会在您的应用程序中引入一个潜在的失败点,并且可能会花费您的钱.
CURL可以很好地做到这一点.这是我在PHP中的表现:
function unshorten_url($url) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => TRUE, // the magic sauce
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors
CURLOPT_SSL_VERIFYPEER => FALSE,
));
curl_exec($ch);
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
Run Code Online (Sandbox Code Playgroud)
我确信这可以适用于其他语言,甚至可以curl
在UNIXy系统上使用该命令编写脚本.
http://jonathonhill.net/2012-05-18/unshorten-urls-with-php-and-curl/
curl -s -o /dev/null --head -w "%{url_effective}\n" -L "https://t.co/6e7LFNBv"
--head
或者-I
只下载HTTP标头-w
或--write-out
在输出后打印指定的字符串-L
或者--location
跟随位置标题如果你想从命令行执行它,curl的详细选项可以解决问题:
curl -v <url>
Run Code Online (Sandbox Code Playgroud)
给你HTTP回复.对于t.co,它似乎给你一个HTTP/301回复(永久移动).然后,有一个位置字段,指向缩短后面的URL.
你可以给unshorten.me一个去吧.它有一个API.
JSON:
http://api.unshort.me/?r=http://theshorturl.ly/28292&t=json
会给你:
{
"requestedURL":"http://theshorturl.ly/28292",
"success":"true",
"resolvedURL":"http://thefullurl.com/a-webiste/what-a-long-url"
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22676 次 |
最近记录: |