jump to navigation

Using a PHP Proxy with Flex to talk Cross Domain May 30, 2010

Posted by Tournas Dimitrios in Flex and PHP.

Now one of the worst pieces of working with Flex is cross domain security, oh how I hate it sometimes :). Now I understand that there is a reason that the Flash player can’t just grab information from any domain it wants (security reasons).Today I am going to show how to build a simple PHP script to use as a cross domain proxy.Below is a picture of the  application to show the working proxy script. The application has two buttons – one for attempting to pull http://www.guidesandtutorials.com feeds directly from the  site and one to retrieve it using the PHP proxy. If there is an error a pop up will show with the error message. It is expected to show an error when not using the proxy, and when using the proxy the rss feed contents should show up in the text area below the buttons.

Creating the PHP Proxy

The major piece of this tutorial is building the PHP script to act as a proxy for our cross domain requests. We are going to use cURL to make the request to the outside site. First and foremost is initializing cURL using Curl_init(). We get the url to retrieve from the requesT variable  which in the Flex application I pass using  POST . We also set a few other options, which can be seen in the code for the proxy below.

$ch = curl_init();
$timeout = 30;
$userAgent = $_SERVER['HTTP_USER_AGENT'];curl_setopt($ch, CURLOPT_URL, $_REQUEST['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);$response = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
} else {
echo $response;

PHP cURL has many options but I just set ones I really needed. This includes the url (CURLOPT_URL), return transfer, (CURLOPT_RETURNTRANSFER), connection timeout (CURLOPT_CONNECTTIMEOUT), and the user agent (CURLOPT_USERAGENT). The url and connection timeout are pretty self-explanatory but what is return transfer and user agent? Well the return transfer is actually a simple concept, it tells cURL to return the information requested as a string and not to output it directly. The user agent is a string that describes the browser and computer of the requester – this is important to make sure the information is returned in the correct format.The last thing that needs to be done in the script is getting the response. This is done using the curl_exec() function which will execute the cURL session and return the response. Once the the session has been executed we can check for any errors and if there are none we close the session and echo out the response. That takes care of the PHP part of the sample.

Building the Flex application

For this part of the tutorial I will explain how I sent the request to the proxy and to the http://www.guidesandtutorials.com site. I am not going to go over all the inner details of building the interface or outputting the rss feed into the text area – all of these things are very typical of a flex application . The requests are fairly simple also , so here is the code and I will go over it afterward.

<mx:httpservice id="noProxyService" method="GET" resultformat="xml" url="http://twitter.com/statuses/user_timeline/13575682.rss" result="rssResults(event)" fault="rssFault(event)"></mx:httpservice>
<mx:httpservice url="/files/proxy.php" id="proxyService" method="POST" resultformat="xml" result="rssResults(event)" fault="rssFault(event)">
    <url> http://twitter.com/statuses/user_timeline/13575682.rss </url>

Now those don’t look too complicated now do they. The first HTTPService is the non-proxy one which is pretty evident from the URL parameter being set to the http://www.guidesandtutorials.com site. Some other items to notice – I ask for the result in xml because it is an xml rss feed – this makes it easier for me to output. I also have two events hooked up, the result and fault events which is where I display the error pop up or the output in the text area. The second HTTPService is slightly more complicated. It has its url parameter set to the PHP proxy file which we built earlier. The other difference is that we also have a request variable ,
URL which is passed using the POST method. The request variable is what I set to the guidesandtutorials.com RSS feed address. To send the requests we simply need to call eithernoProxyService.send() or proxyService.send() and that’s it.
If anyone has any questions about the Flex or PHP code please feel free to leave a comment . I’am always more than happy to help out when time permits. Also you can download the full source code for the Flex application and the php-script if you would like.

The final application as it was tested on my local wamp server is demonstrated on the following two pictures .

NOTE : I had to enable the curl-extension on my wamp server so  this application can working (wamp–> PHP –> PHP extensions –> click on PHP_curl)

Without the proxy server :Flash enables the sandbox security restriction polity


With the PHP enabled proxy feature



1. Security error accessing Flash content from other domain « Tournas Dimitrios - July 30, 2010

[…] Using a proxy page. You place the proxy page on your site and then you call it. It then gets the remote sites page and passes that back to your page on your server. There are numerous examples online. Here is an article from my blog >>> […]

2. Parsing RSS 2.0 Feeds in ActionScript 3 « Tournas Dimitrios - October 14, 2010

[…] enabled , otherwise you must implement a proxy solution like PHP and MagpieRSS . See also Using a PHP Proxy with Flex to talk Cross Domain […]

3. Get Your IP with JQuery « Tournas Dimitrios - December 26, 2011

[…] ” function . An other method to access data from an remote domain is by implementing a “proxy-server” on your web-host .  Ajax requests are under the same limitations , you cannot do […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s