Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 09-22-2008, 07:43 PM RSS to JSON
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
I posted this on another forum too, but thought I would ask here as well. I'm working on an RSS reader and I'm running into a little snag, so I thought I'd ask to see what people thought the best approach might be. I want to convert it to JSON before I turn it over to the JavaScript, so I'm using the simplexml and JSON functions. It's working okay except that it also converts any embedded HTML into JSON as well, which makes it hard for the JavaScript class I wrote for it. What I'm thinking about doing is entity-encoding everything inside the (RSS) description tag, but I'm not quite sure the best way to do that or if that's the best solution because I might run into some problems if the feed is about HTML or JavaScript. Any suggestions?

Here's my code so far:

PHP Code:
$url urldecode($_POST['url']);
$rawFeed file_get_contents($url);
$rawFeed preg_replace('#<!\[CDATA\[(.*)\]\]>#'''$rawFeed);
$feed simplexml_load_string($rawFeed);
echo 
json_encode($feed); 
On a side note, should I be stripping script tags as well? Also, if you have any further suggestions for this, I'd also like to hear them. Thanks.
__________________
Want new web resources every day? - Follow me on
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
 
Register now for full access!
Old 09-22-2008, 11:33 PM Re: RSS to JSON
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Does this help?

PHP Code:
<html>
  <head>
    <title>Test RSS to JSON</title>
    <script>
    <?php      
      $rss_contents 
file_get_contents($filename_or_url);
      
$rss_xml_contents = new SimpleXMLElement($rss_contents);
      
      
$json_contents ''$json_prefix1 ''$json_prefix2 '';
      if (!empty(
$rss_xml_contents->channel->item)) {
        
        
$json_contents .= '{"item":[';
        foreach (
$rss_xml_contents->channel->item as $item_details) {
          
$json_contents .= $json_prefix1.'{';
          foreach (
$item_details as $tag_name=>$tag_value) {
            
$json_contents .= $json_prefix2.'"'.$tag_name.'":"'.str_replace('"','\"',$tag_value).'"';
            
$json_prefix2 ",\n";
          }    
          
$json_contents .= '}';
          
$json_prefix1 ",\n";
          
$json_prefix2 "";
        }
        
$json_contents .= ']}';
      }
      echo 
'var a_rss_feed = eval('.$json_contents.');';
    
?>
    </script>
  </head>
  <body>
    <script>
      for (i=0;i<a_rss_feed.item.length;i++) {
       document.write('<a href="'+a_rss_feed.item[i].link+'">'+a_rss_feed.item[i].title+'</a><p>'+a_rss_feed.item[i].description+'</p>');
      }
    </script>
  </body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to RSS to JSON
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47740 seconds with 12 queries