Using a PHP Proxy with Flex to talk Cross Domain
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 all willy nilly, but sometimes this really hampers getting something built. Silverlight also has this issue, and recently I went over using Yahoo Pipes for a proxy for a Silverlight Twitter Client. Today I am going to show how to build a simple PHP script to use as a cross domain proxy.
|
Below is a simple example application to show the working proxy script. The application has two buttons - one for attempting to pull my (Zwigby's) twitter rss feed directly from the twitter 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 url 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 {
curl_close($ch);
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 final 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
For this part of the tutorial I will explain how I sent the request to the proxy and to the normal twitter 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 and we have plenty of Flex Tutorials here on the site. The requests are fairly simple also so here is the code and I will go over it afterward.
id="noProxyService" method="GET" resultFormat="xml"
url="http://twitter.com/statuses/user_timeline/13575682.rss"
result="rssResults(event)" fault="rssFault(event)" />
<mx:HTTPService
id="proxyService" method="POST" resultFormat="xml"
url="/files/proxy.php"
result="rssResults(event)" fault="rssFault(event)">
<mx:request>
<url>http://twitter.com/statuses/user_timeline/13575682.rss
</url>
</mx:request>
</mx:HTTPService>
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 twitter 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 Twitter RSS feed address. To send the requests we simply need to call either noProxyService.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 or send us an email. We are always more than happy to help out when time permits. Also you can look at the full source code for the Flex application if you would like.
Posted in PHP, Flex, All Tutorials by The Fattest |

July 3rd, 2008 at 10:15 am
I just had this really annoying problem today (I’ve a little few day experiment in flex ;), and used the same solution. But not with curl which seems more robust than my poor “fread()”.
You just confirm me that’s the only solution. Thanks.
July 4th, 2008 at 3:06 am
What about for Java? Do you have knowledge how to do in say a Java Servlet as a proxy?