2012-02-24 14 views
1

私は次のコードを使用してGoogleのニュースを取り込んでいますが、Webサイトの最終結果を実際のRSSフィードにして他のユーザーがフィードを取得できるようにする必要があります。今すぐ出力が素晴らしいindex.phpページを作成します。それは素晴らしいですが、私の目的に合っていません。 SimplePieは、取得目的でrss出力としてフォーマットされたページを作成できますか?電子メールでのバンドル

ありがとうございます。


<?php 

// Make sure SimplePie is included. You may need to change this to match the location of simplepie.inc. 
require_once('simplepie.inc'); 

// We'll process this feed with all of the default options. 
$feed = new SimplePie(); 

// Set the feed to process. 
$feed->set_feed_url('http://news.google.com/news?hl=en&gl=us&q=new+york+commercial+real+estate&ie=UTF-8&output=rss'); 

// Run SimplePie. 
$feed->init(); 

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it). 
$feed->handle_content_type(); 

// Let's begin our XHTML webpage code. The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag. 
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
    <title>Sample SimplePie Page</title> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 

</head> 
<body> 

     <div class="header"> 
     <h2><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h2> 
     <p><?php echo $feed->get_description(); ?></p> 
    </div> 

    <?php 
    /* 
    Here, we'll loop through all of the items in the feed, and $item represents the current item in the loop. 
    */ 
    foreach ($feed->get_items() as $item): 
    ?> 

     <div class="item"> 
      <h4><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h4> 
      <p><?php echo $item->get_description(); ?></p> 
      <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p> 
     </div> 

    <?php endforeach; ?> 

</body> 
</html> 
+0

FYI、私はあなたのサービス利用規約に違反していると記載している方法でGoogleニュースを再パッケージすると考えています。 – Brad

答えて

0

はSimplePieのみ構文解析段階のために使用されている、あなたはRSSとして出力することを独自のコードを記述する必要があります。

あなたはそれを迅速かつ汚い方法で行うことができます。

<?xml version="1.0" encoding="UTF-8" ?> 
<rss version="2.0"> 
    <channel> 
     <title>Your Site</title> 

<?php 
     foreach ($feed->get_items() as $item): 
?> 
     <entry> 
      <title><?php echo $item->get_title() ?></title> 
      <description><?php echo $item->get_content() ?></description> 
      <!-- ... --> 
     </entry> 
    </channel> 
</rss> 

をもっと良い方法は、要素と、出力その結果を作成するためのSimpleXMLを使用することです。そうすれば、XMLが整形式であり、すべてが正しくエスケープされます。

しかし、ブラッド氏が述べたように、これを行う際にはサービス条件に注意する必要もあります。少なくとも、適切な帰属を保証する必要があります。

+0

ありがとうございます。私はサービス利用規約に違反することに非常に懸念しています。私はサービスの条件を読みましたが、適切な帰属を保証すれば、それは問題にはならないと思われますが、明らかに不透明なように、これについての議論は確かです。コードスニペットについては、提案のおかげで、私はそれを試してみましょう。 –

+0

Ryanが提供するクイックシンプルについては、SimplePieに統合されるコードか、SimplePieから完全に分離された独立したコードですか?私は、$ item-> get_title()がsimplepieライブラリコードのように見え、フィードURLへの参照を見つけることができません。ありがとうございました。 –

+0

そのコードはフィードを出力したい場所に移動します。上記の例では、「XHTML Webページコードを開始しましょう」の後にあるすべてのものを置き換えます。これは非常に簡単な例でもありますが、完全なフィードにはもっと多くの情報が含まれていますが、これは単にタグをSimplePieメソッドにマッチさせる場合です。 –

関連する問題