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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?