Tuesday, December 30, 2008

Flex Security error accessing url

This was a problem I encountered while trying to load an external RSS-feed with the HTTP Service in Flex 3. Amazingly I couldn't really find any good solution to this problem when googling it. Just alot about configuring some crossdomain.xml file that is supposed to be in the root directory of the DATA providing server - which really doesn't help since I don't have access to it.

So, the problem here is that the rss-feed is on another server, and that you cannot add a crossdomain.xml to that other server's root directory, right? So, is there some way we could move that feed to your own server, still having it dynamic? Yes there is!

What I ended up doing was making a script in PHP that loads the other site's feed, and then just print it, as such:


<?php

$filename = 'http://www.path.to/the/rss/feed.xml';
$handle = fopen($filename, 'r');
$contents = stream_get_contents($handle);
fclose($handle);
echo $contents;

?>



And then in the Flex application, I just load that PHP-page instead (located on the same domain):


<mx:HTTPService url="http://www.myowndomain.com/thePhpFile.php" resultformat="object">



Very ugly, yes, but it works. Make sure you reload the page "for real" (either clear browser data or use ctrl + F5) when visiting the page again.

If this really is the way to go, then preferably the php-file should also cache the data fetched from the other server.

NOTE that you still need the crossdomain.xml, but on YOUR server.


<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>




I hope this has helped you.

2 comments:

  1. This does it! I thought I could do a Flex gadget consuming the RSS feeds from other sites, and now I realise it can't be done (my web host doesn't provide PHP options :-( )

    ReplyDelete
  2. Thanks so much. Your post is very helpful. Please keep going!!!

    ReplyDelete