<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The bAS3 Class &#187; web services</title>
	<atom:link href="http://www.brandondement.com/blog/tag/web-services/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brandondement.com/blog</link>
	<description>Adventures in software development</description>
	<lastBuildDate>Thu, 29 Sep 2011 23:31:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using a Simple PHP Proxy to Load Data Across Domains</title>
		<link>http://www.brandondement.com/blog/2009/08/23/using-a-simple-php-proxy-to-load-data-across-domains/</link>
		<comments>http://www.brandondement.com/blog/2009/08/23/using-a-simple-php-proxy-to-load-data-across-domains/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 23:53:56 +0000</pubDate>
		<dc:creator>Brandon</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[crossdomain]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://www.brandondement.com/blog/?p=26</guid>
		<description><![CDATA[The Flash Player doesn&#8217;t allow you to load and use data from other domains, even a subdomain of where you SWF is being served from.  Normally this can be worked around by editing the policy file, crossdomain.xml, of the domain where the data is located, but what can you do when the data you [...]]]></description>
			<content:encoded><![CDATA[<p>The Flash Player doesn&#8217;t allow you to load and use data from other domains, even a subdomain of where you SWF is being served from.  Normally this can be worked around by editing the policy file, crossdomain.xml, of the domain where the data is located, but what can you do when the data you want to load is owned by someone like Facebook or Twitter?  Chances are that you don&#8217;t have the kind of pull it would take to get them to change their policy files, so it&#8217;s time to implement a web service on the domain where the SWF is hosted.</p>
<p>I ran into this problem while working with the Twitter and Facebook APIs on my site.  Their APIs allow you to get names, URLs, and other interesting data points, but I wanted to load images, which aren&#8217;t delivered through the API.  To solve this problem, I wrote this dead-simple PHP script which simply forwards GET requests to a URL specified in the &#8220;url&#8221; parameter and displays the result back to the requester:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
     <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> 
     <span style="color: #009900;">&#123;</span>   
          <span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span> 
     <span style="color: #b1b100;">else</span> 
     <span style="color: #009900;">&#123;</span>  
          <span style="color: #666666; font-style: italic;">// there was an error</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Of course this is the most simple of examples of a web service that you can get, and is really only useful for simple content like <a href="http://www.brandondement.com/proxy.php?url=http://twitter.com/crossdomain.xml" target="_blank">XML</a>, <a href="http://www.brandondement.com/proxy.php?url=http://www.google.com" target="_blank">HTML</a> and <a href="http://www.brandondement.com/proxy.php?url=http://photos-f.ak.fbcdn.net/hphotos-ak-snc1/hs130.snc1/5570_625691532119_12805853_36162773_4932061_n.jpg" target="_blank">images</a>.  Anything more complicated than this and you would want to create a proper web service with input validation, caching, etc., but this is a quick way to get up and running. </p>
<p>Note that your browser won&#8217;t render the proxy-ed image correctly because it doesn&#8217;t receive the correct content-type header, which tells the browser to expect an image.  What you&#8217;re seeing is actually the ASCII representation of the image&#8217;s binary data, known as <code>BitmapData</code> to the AS3 world.</p>
<p>Bonus!  It can be slow to go through your proxy when not needed, so I use this simple class to create <code>URLRequests</code> with the proper URL as determined by the SWF&#8217;s security sandbox:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">bdement</span>.<span style="color: #006600;">proxy</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">system</span>.<span style="color: #006600;">Security</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProxyService
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const PROXY_URL:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;http://www.brandondement.com/proxy.php?url=&quot;</span>;		
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ProxyService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;All methods of ProxyService are static, do not instantiate!&quot;</span><span style="color: #66cc66;">&#41;</span>;	
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getProxyRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> : URLRequest
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span> == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>isLocal ? <span style="color: #ff0000;">&quot;&quot;</span> : PROXY_URL<span style="color: #66cc66;">&#41;</span> + <span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> toProxySource<span style="color: #66cc66;">&#40;</span>source:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span>
	<span style="color: #66cc66;">&#123;</span>			
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>source == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>isLocal ? <span style="color: #ff0000;">&quot;&quot;</span> : PROXY_URL<span style="color: #66cc66;">&#41;</span> + source;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> isLocal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Boolean</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span>	<span style="color: #66cc66;">&#40;</span>	Security.<span style="color: #006600;">sandboxType</span> == Security.<span style="color: #006600;">LOCAL_TRUSTED</span> 
			<span style="color: #66cc66;">||</span>	Security.<span style="color: #006600;">sandboxType</span> == Security.<span style="color: #006600;">LOCAL_WITH_NETWORK</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> 
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brandondement.com/blog/2009/08/23/using-a-simple-php-proxy-to-load-data-across-domains/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

