drkbpwk609294 2014-11-17 19:32
浏览 45

如何从URL中找出实际的URL,重定向到另一个URL

From an RSS feed of a news website, I get a list of articles published. As this website is using feedburner for RSS, each article has another URL which redirects to the actual URL of an article. I need to get the actual URL and using this URL I'll get the total FB shares and tweets of this article.

Sample RSS feed: http://feeds.hindustantimes.com/HT-HomePage-TopStories. This feed has an article - http://feeds.hindustantimes.com/~r/HT-HomePage-TopStories/~3/Go3sAI0Bv94/story01.htm which redirects to actual URL - http://www.hindustantimes.com/india-news/mumbai/complaint-against-fake-twitter-accnts-of-party-chief-s-kids/article1-1287174.aspx.

Is there any way PHP I can get the actual URL?

  • 写回答

1条回答 默认 最新

  • duan1226 2014-11-17 20:11
    关注

    You can use either get_headers, or curl with the flag CURLOPT_FOLLOWLOCATION set in order to grab redirects. The following snippet uses get_headers.

    function get_redirected_url($url)
    {
        $redirected_url = false;
    
        // First check response headers
        $headers = get_headers($url);
    
        // Test for 301 or 302
        if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0]))
        {
            foreach($headers as $value)
            {
                if(substr(strtolower($value), 0, 9) == "location:")
                {
                    $redirected_url = trim(substr($value, 9, strlen($value)));
                }
            }
        }
    
        return ($redirected_url) ? $redirected_url : $url;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据