2011-08-06 22 views

答えて

3

これは、ここにコードを貼り付けるよりもはるかに複雑です。しかし、私はあなたがする必要があることについて正しい方向にあなたを指すことができます。

  1. まず、あなたが戻ってRSS Autodiscovery Meta tag探します文字列を解析ページ
  2. を取得する必要があります。ドキュメント全体をXMLとしてマップしたり、DOMのトラバーサルを使用することもできますが、正規表現を使用するだけです。
  3. タグのhref部分を抽出すると、RSSフィードのURLを取得できます。
+0

こんにちは、あなたはrssのフィードURLを識別するためのhtmlソースのスクラップについて言及していますか? – Jeyaganesh

1

[PHPまたはjqueryのいずれかを使用して]

。あなたは、HTMLを解析し、記述された要素を探す必要があります。

13
一般的なプロセスが既に回答されている

QuentinDOOManiac)ので、いくつかのコード(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)

+0

優秀!それは私のために非常にうまく動作します – fortytwo

1

rssでもatomでも、最初の利用可能なフィードを取得する機能はわずかです(ほとんどのブログには2つのオプションがあります - これは最初の設定を取得します)。

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; 
    } 
関連する問題