Wednesday, March 12, 2008

Overcoming cross domain issues through php proxy server in Flex for RSS Reader samples

Today, I took a second look at my home page and decided that the blog link should not directly point to this page and should have an in-built RSS Reader for getting these posts there. And then the troubles started. People who know me already know my website and know that it was built in Flex. I had this idea of an in-built RSS Reader in my home page for long, but whenever I started working on it, I would have weird exceptions on page load, which were not straight forward and I wouldn't have time to debug them. So it remained the same, a hyperlink to this blog. Today a post on DZone caught my attention and I again tried this. I found a simple example which I implemented in my home page and thankfully everything went cool. But when I put it on the server, another problem started. The cross-domain problem..

The thing about flex is that, while being cool and xml dominated, the biggest problem is with the fact that you cannot load the rss or xml from other websites, without a cross-domain.xml file on the serving domain that lists the calling domain. Now I cannot ask blogger.com to put my domain name in their cross-domain.xml file. Can I ?? So I preferred the alternative route, which is widely written on the net, the proxy server route. And though I do not have Java hosting by my space provider, they support php and went for a simple php script..

<?php
header('Content-Type: text/xml');
$url = 'http://cognitivecache.blogspot.com/rss.xml';
$content = file_get_contents($url);
echo $content;
?>

The script looks so simple like a no-brainer right. Nope, that wouldn't still solve my problem. Because, the blogger wouldn't let the php script open a stream and I kept ending up with this error

Warning: readfile [function.readfile]: failed to open stream: No route to host..

After changing the scripts from the net thrice hoping the other one would be different, I kept coming back to the same problem. Finally I realized that it could be an issue with Blogger not allowing an incoming connection. Boom, that was it. I changed the code to point to my feed in FeedBurner and that was it. My new code looks like this

<?php
header('Content-Type: text/xml');
$url = 'http://feeds.feedburner.com/CognitiveCache';
$content = file_get_contents($url);
echo $content;
?>

Now, The RSS Reader gets its content now and it looks like a nice addition. A Happy ending to lot of time spent indeed !!

If you are still wondering about the whole process, what you need to do, is to burn a feed in FeedBurner and put the above code in a file called proxy.php. Now upload this file to your domain and use the url for xml in your flash file as "http://mydomain.com/proxy.php" and thats it..

6 comments:

Seshu Karthick said...

Dude... I was able to access my feedburner feeds directly from flex without a proxy. May be, you require a proxy only for accessing the real feed (eg. abc.blogspot.com/rss.xml)

Saagar said...

I didn't try feedburner directly Seshu, may be feedburner has a * in its cross-domain.xml allowing any flex based RSS readers to pick up the feeds..

Vanessa du Frat said...

Hi! Just wanted to say a big thank you for this post. I've been struggling for almost an hour trying to get my facebook status rss feed into flash, and finally, I passed it through feedburner, and here we go :)

Filosofi said...

Yup, you're the the man. This worked great first try. As a note I was able to use it to directly access any rss feed, doesn't need to be feedburner. For work I had to build a flash rss reader to get feeds from autonews, motortrend and autoweek. This proxy trick was magic.

Thank You!

axliz said...

really nice post
hi dude, i want to change my crossdomain.xml into mine.
but i have trouble to find the place where to change.

can you help me explain how to change crossdomain.xml in blogspot.

i am really confused.

thanks

Anonymous said...

Thank you so much. This is the simplest and most elegant solution to the problem I found on the net. it works perfectly.

vs