如果我有一个Twitter .co链接,我如何在php中取消它?
简单示例:
$ch = curl_init("http://t.co/...");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$yy = curl_exec($ch);
curl_close($ch);
$w = explode("'n",$yy);
$real_url = substr($w[3],10); # the fourth line is "Location: http://..."
echo $real_url;
您将需要使用cURL(带有CURLOPT_HEADER
选项)来获取URL的标头并查找Location:
标头。
我建议使用CURLINFO_EFFECTIVE_URL
和curl_getinfo()
。
见https://stackoverflow.com/a/10661246/168815