dqqlziv195281 2013-01-31 07:27
浏览 101
已采纳

从双重缩短的URL获取最终URL(t.co - > bit.ly - > final)

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: (.*)
/', $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?

  • 写回答

4条回答 默认 最新

  • donglipi4495 2013-02-01 06:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧