在 PHP 或 JS 中扩展 Bitly、Tinyurl 等缩短的 URL 来查找原始 URL 的最佳方法是什么?

发布于 2024-10-08 13:23:46 字数 122 浏览 2 评论 0原文

我正在使用 Twitter 和 Facewbook API 来提取可能包含使用 bit.ly 或 TinyURL 之类服务的缩短 URL 的帖子。我需要进行实时扩展以获取原始 URL,然后将内容从该 URL 提取到我的应用程序中。

I am using the Twitter and Facewbook API to pull posts that potentially contain shortened URLs using bit.ly or TinyURL like services. I need to do a real-time expansion to get the original URL then pull content from that URL into my app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2024-10-15 13:23:47

对于nodejs,您可以使用模块request

var request = require('request');
var shortUrl = 'the url that is shortened'
request({method: 'HEAD', url: shortUrl, followAllRedirects: true}, 
  function(err, response, body){
     console.log(response.request.href);
  })

With nodejs you can use the module request.

var request = require('request');
var shortUrl = 'the url that is shortened'
request({method: 'HEAD', url: shortUrl, followAllRedirects: true}, 
  function(err, response, body){
     console.log(response.request.href);
  })
眼波传意 2024-10-15 13:23:47

我找到了一个 php 库,它可以做到这一点,它很有用。
看看:https://launchpad.net/longurl

I found a php library that does just that, it can be useful.
Check it out: https://launchpad.net/longurl

原来是傀儡 2024-10-15 13:23:46

您可以使用 CURL 来扩展短 URL。

试试这个:

    function traceUrl($url, $hops = 0)
    {
        if ($hops == MAX_URL_HOPS)
        {
            throw new Exception('TOO_MANY_HOPS');
        }

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $r = curl_exec($ch);

        if (preg_match('/Location: (?P<url>.*)/i', $r, $match))
        {
            return traceUrl($match['url'], $hops + 1);
        }

        return rtrim($url);
    }

您可以像 traceUrl('http://bit.ly/example') 一样使用此函数。这个函数是递归的,因为它甚至会找到被缩短的短网址(如果发生过的话)。确保设置 MAX_URL_HOPS 常量。我使用define('MAX_URL_HOPS', 5);

  • 基督教

You can use CURL to expand a short URL.

Try this:

    function traceUrl($url, $hops = 0)
    {
        if ($hops == MAX_URL_HOPS)
        {
            throw new Exception('TOO_MANY_HOPS');
        }

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $r = curl_exec($ch);

        if (preg_match('/Location: (?P<url>.*)/i', $r, $match))
        {
            return traceUrl($match['url'], $hops + 1);
        }

        return rtrim($url);
    }

You can use this function as so traceUrl('http://bit.ly/example'). This function is recursive in the sense that it will even find short urls that are shortened (if it ever happens). Make sure you set the MAX_URL_HOPS constant. I use define('MAX_URL_HOPS', 5);.

  • Christian
古镇旧梦 2024-10-15 13:23:46

您可以使用 PHP 和 CURL 连接到 URL 并返回 Location 参数:

以下是返回的内容 -

> $ curl -I http://bit.ly/2V6CFi
> HTTP/1.1 301 Moved Server:
> nginx/0.7.67 Date: Tue, 21 Dec 2010
> 01:58:47 GMT Content-Type: text/html;
> charset=utf-8 Connection: keep-alive
> Set-Cookie:
> _bit=4d1009d7-00298-02f7f-c6ac8fa8;domain=.bit.ly;expires=Sat
> Jun 18 21:58:47 2011;path=/; HttpOnly
> Cache-control: private; max-age=90
> Location: http://www.google.com/
> MIME-Version: 1.0

Content-Length: 284

因此您可以在标头中查找 Location 参数以查看页面实际位置去。

You can just use PHP and CURL to connect to the URL and get back the Location parameter:

Here is what comes back -

> $ curl -I http://bit.ly/2V6CFi
> HTTP/1.1 301 Moved Server:
> nginx/0.7.67 Date: Tue, 21 Dec 2010
> 01:58:47 GMT Content-Type: text/html;
> charset=utf-8 Connection: keep-alive
> Set-Cookie:
> _bit=4d1009d7-00298-02f7f-c6ac8fa8;domain=.bit.ly;expires=Sat
> Jun 18 21:58:47 2011;path=/; HttpOnly
> Cache-control: private; max-age=90
> Location: http://www.google.com/
> MIME-Version: 1.0

Content-Length: 284

So you can look for the Location parameter in the header to see where the page page actually goes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文