【问题标题】:How to fetch rss feed url of a website using php?如何使用 php 获取网站的 rss feed url?
【发布时间】:2011-08-06 16:21:00
【问题描述】:

我需要以编程方式查找网站的 rss 提要 url。

[使用php或jquery]

【问题讨论】:

    标签: php rss


    【解决方案1】:

    大体流程已经回答(Quentin,DOOManiac),所以一些代码(Demo):

    <?php
    
    $location = 'http://hakre.wordpress.com/';
    $html = file_get_contents($location);
    echo getRSSLocation($html, $location); # http://hakre.wordpress.com/feed/
    
    /**
     * @link http://keithdevens.com/weblog/archive/2002/Jun/03/RSSAuto-DiscoveryPHP
     */
    function getRSSLocation($html, $location){
        if(!$html or !$location){
            return false;
        }else{
            #search through the HTML, save all <link> tags
            # and store each link's attributes in an associative array
            preg_match_all('/<link\s+(.*?)\s*\/?>/si', $html, $matches);
            $links = $matches[1];
            $final_links = array();
            $link_count = count($links);
            for($n=0; $n<$link_count; $n++){
                $attributes = preg_split('/\s+/s', $links[$n]);
                foreach($attributes as $attribute){
                    $att = preg_split('/\s*=\s*/s', $attribute, 2);
                    if(isset($att[1])){
                        $att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]);
                        $final_link[strtolower($att[0])] = $att[1];
                    }
                }
                $final_links[$n] = $final_link;
            }
            #now figure out which one points to the RSS file
            for($n=0; $n<$link_count; $n++){
                if(strtolower($final_links[$n]['rel']) == 'alternate'){
                    if(strtolower($final_links[$n]['type']) == 'application/rss+xml'){
                        $href = $final_links[$n]['href'];
                    }
                    if(!$href and strtolower($final_links[$n]['type']) == 'text/xml'){
                        #kludge to make the first version of this still work
                        $href = $final_links[$n]['href'];
                    }
                    if($href){
                        if(strstr($href, "http://") !== false){ #if it's absolute
                            $full_url = $href;
                        }else{ #otherwise, 'absolutize' it
                            $url_parts = parse_url($location);
                            #only made it work for http:// links. Any problem with this?
                            $full_url = "http://$url_parts[host]";
                            if(isset($url_parts['port'])){
                                $full_url .= ":$url_parts[port]";
                            }
                            if($href{0} != '/'){ #it's a relative link on the domain
                                $full_url .= dirname($url_parts['path']);
                                if(substr($full_url, -1) != '/'){
                                    #if the last character isn't a '/', add it
                                    $full_url .= '/';
                                }
                            }
                            $full_url .= $href;
                        }
                        return $full_url;
                    }
                }
            }
            return false;
        }
    }
    

    请参阅:RSS auto-discovery with PHP (archived copy)

    【讨论】:

      【解决方案2】:

      这比仅仅在此处粘贴一些代码涉及更多内容。但我可以为您指明正确的方向。

      1. 首先您需要获取页面
      2. 解析返回的字符串以查找RSS Autodiscovery Meta tag。您可以将整个文档映射为 XML 并使用 DOM 遍历,但我只会使用正则表达式。
      3. 提取标签的 href 部分,您现在就有了 RSS 提要的 URL。

      【讨论】:

      • 您好,您是否提到了通过 html 源代码抓取来识别 rss feed url?
      【解决方案3】:

      rules for making RSS discoverable 有很好的文档记录。您只需要解析 HTML 并查找所描述的元素。

      【讨论】:

        【解决方案4】:

        一个稍微小一点的函数,它将获取第一个可用的提要,无论是 rss 还是 atom(大多数博客都有两个选项 - 这会获取第一个首选项)。

        public function getFeedUrl($url){
                if(@file_get_contents($url)){
                    preg_match_all('/<link\srel\=\"alternate\"\stype\=\"application\/(?:rss|atom)\+xml\"\stitle\=\".*href\=\"(.*)\"\s\/\>/', file_get_contents($url), $matches);
                    return $matches[1][0];
                }
                return false;
            }
        

        【讨论】:

          猜你喜欢
          • 2012-10-04
          • 1970-01-01
          • 2015-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多