我不能双缩短URL转换为扩展URL成功地使用下面的函数,我从拿到这里 :
function doShortURLDecode($url) {
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = @curl_exec($ch);
preg_match('/Location: (.*)\n/', $response, $a);
if (!isset($a[1])) return $url;
return $a[1];
}
我遇到了麻烦,当我展开的URL又是一个缩短的URL,它有它的展开网址。
如何获得最终的膨胀URL它通过两个网址缩短服务运行后?
由于t.co
使用HTML重定向通过使用JavaScript和/或中<meta>
重定向,我们需要抓住它首先是内容。 然后提取bit.ly
URL从它来执行HTTP报头的请求,以获得最终位置。 在服务器上启用该方法不依赖于卷曲并使用所有本地PHP5的功能:
测试和工作!
function large_url($url)
{
$data = file_get_contents($url); // t.co uses HTML redirection
$url = strtok(strstr($data, 'http://bit.ly/'), '"'); // grab bit.ly URL
stream_context_set_default(array('http' => array('method' => 'HEAD')));
$headers = get_headers($url, 1); // get HTTP headers
return (isset($headers['Location'])) // check if Location header set
? $headers['Location'] // return Location header value
: $url; // return bit.ly URL instead
}
// DEMO
$url = 'http://t.co/dd4b3kOz';
echo large_url($url);
终于找到一种方式来获得一个双缩短网址的最终网址。 最好的办法是使用longurl API它。
我不知道这是否是正确的做法,但我终于能输出作为所需要的最终网址:)
这是我做的:
<?php
function TextAfterTag($input, $tag)
{
$result = '';
$tagPos = strpos($input, $tag);
if (!($tagPos === false))
{
$length = strlen($input);
$substrLength = $length - $tagPos + 1;
$result = substr($input, $tagPos + 1, $substrLength);
}
return trim($result);
}
function expandUrlLongApi($url)
{
$format = 'json';
$api_query = "http://api.longurl.org/v2/expand?" .
"url={$url}&response-code=1&format={$format}";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $api_query );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
$fileContents = curl_exec($ch);
curl_close($ch);
$s1=str_replace("{"," ","$fileContents");
$s2=str_replace("}"," ","$s1");
$s2=trim($s2);
$s3=array();
$s3=explode(",",$s2);
$s4=TextAfterTag($s3[0],(':'));
$s4=stripslashes($s4);
return $s4;
}
echo expandUrlLongApi('http://t.co/dd4b3kOz');
?>
输出我得到的是:
"http://changeordie.therepublik.net/?p=371#proliferation"
上面的代码工作。
该@cryptic共享的代码也是正确的 ,但我不能让我的(也许是因为一些配置问题)服务器上的结果。
如果有人认为,它可以通过一些其他的方式进行,请随时分享。
也许你应该只使用CURLOPT_FOLLOWLOCATION
= true,然后确定您被定向到最终的URL。
如果问题不是一个JavaScript重定向在t.co或<META http-equiv="refresh"...
,这是reslolving stackexchange网址像https://stackoverflow.com/q/62317
罚款:
public function doShortURLDecode($url) {
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = @curl_exec($ch);
$cleanresponse= preg_replace('/[^A-Za-z0-9\- _,.:\n\/]/', '', $response);
preg_match('/Location: (.*)[\n\r]/', $cleanresponse, $a);
if (!isset($a[1])) return $url;
return parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST).$a[1];
}
它清除的任何特殊字符的响应,可以在cuttoing出来的结果网址前的卷曲输出发生(我遇到了一个php7.3服务器上的这个问题)