从双缩短网址获取最终到达网址(t.co - > bit.ly - > final) [英] Get Final URL From Double Shortened URL (t.co -> bit.ly -> final)

查看:171
本文介绍了从双缩短网址获取最终到达网址(t.co - > bit.ly - > final)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用以下函数将双缩短网址成功转换为展开网址:此处

  function doShortURLDecode ){
$ 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:(。*)\\\
/',$ response,$ a);
if(!isset($ a [1]))return $ url;
return $ a [1];
}



当我获得的扩展URL是一个缩短的URL时,



如何在运行完两个网址缩短服务后获取最终展开的网址?

解决方案

最后找到一种方法来获取一个双缩短网址的最终网址。最好的方法是使用longurl api。



我不知道是否是正确的方法,但我最后得到的输出作为最终的url需要:)



这是我做的:

 <?php 
function TextAfterTag($ input,$标签)
{
$ result ='';
$ tagPos = strpos($ input,$ tag);

if(!($ tagPos === false))
{
$ length = strlen($ input);
$ substrLength = $ length - $ tagPos + 1;
$ result = substr($ input,$ tagPos + 1,$ substrLength);
}

return trim($ result);
}

函数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共享的代码也是正确,但我无法获得结果在我的服务器上(可能是因为一些配置问题)。



如果任何人认为可以通过其他方式做,请随意分享。 >

I couldn't convert a double shortened URL to expanded URL successfully using the below function I got from here:

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];
    }

I got into trouble when the expanded URL I got was again a shortened URL, which has its expanded URL.

How do I get final expanded URL after it has run through both URL shortening services?

解决方案

Finally found a way to get the final url of a double shortened url. The best way is to use longurl api for it.

I am not sure if it is the correct way, but i am at last getting the output as the final url needed :)

Here's what i did:

<?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');
?>

The output i get is:

"http://changeordie.therepublik.net/?p=371#proliferation"

The above code works.

The code that @cryptic shared is also correct ,but i could not get the result on my server (maybe because of some configuration issue).

If anyone thinks that it could be done by some other way, please feel free to share it.

这篇关于从双缩短网址获取最终到达网址(t.co - &gt; bit.ly - &gt; final)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆