FLEX MASHUPS
335
As you can see from the previous example, you make a service call to retrieve the XML and once you
retrieve the results You set the name space:
var georss:Namespace = new Namespace("http://www.georss.org/georss");
Yoou can than access the results.
Creating a Proxy
The lack of crossdomain.xml can create a scenario where your Flex application works fine on your local
machine but not on your server. The solution for the lack of a crossdomain.xml file is to use a proxy to
connect to the data source and have Flash connect to the proxy instead of the data source directly. By
connecting to a proxy instead of the service, you can access services that don’t allow direct access for
Flash. Web pages can access other open APIs and expose the data for the Flash player. The reason that
using proxy works is that the Flash player allows accessing services from the same domain without the
need to install a cross-domain policy.
There are a few ways to use proxies; one is to create a server-side script to connect to the data source.
Following are three open source examples of proxies that can be used for RESTful POST and GET
methods, as well as for downloading binary data such as SWF files from a remote server. The proxy
scripts will be available for download from the friendsofED site (www.friendsofed.com) as part of the
example files that accompany this book.
PHP proxy
When using a server with PHP, you can usea lightweight open source PHP proxy script that handles two
types of data: RESTful methods of GET and PUT and binary data such as SWF files or images. Keep in
mind that PHP supports the CURL method in order for this script to work.
The following example loads XML/text:
http://yourserver.com/proxy.php?url=
http://yourserver.com/blog/index.xml
This example loads a SWF (binary data) file:
http://yourserver.com/proxy.php?url=
http://yourserver.com/files/some.swf&
mimeType=application/x-shockwave-flash
Here’s the complete code:
<?php
// PHP Proxy
// Responds to both HTTP GET and POST requests
// Author: Abdul Qabiz
// March 31st, 2006
// Get the url of to be proxied
$url = ($_POST['url']) ? $_POST['url'] : $_GET['url'];
$headers = ($_POST['headers'])?$_POST['headers']:$_GET['headers'];
$mimeType =($_POST['mimeType'])?$_POST['mimeType']:
$_GET['mimeType'];
CHAPTER 10
336
//Start the Curl session
$session = curl_init($url);
// If it's a POST, put the POST data in the body
if ($_POST['url']) {
$postvars = '';
while ($element = current($_POST)) {
$postvars .= key($_POST).'='.$element.'&';
next($_POST);
}
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
}
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session,CURLOPT_HEADER,($headers=="true")?true:false);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$response = curl_exec($session);
if ($mimeType != "")
{
// The web service returns XML.
// Set the Content-Type appropriately
header("Content-Type: ".$mimeType);
}
echo $response;
curl_close($session);
?>
You first get the URL using either POST or GET, then you set the curl request and make the call and
return the XML.
ASP Proxy
When using a server with ASP technology, you can use a lightweight open source ASP proxy script. This
proxy can be used for RESTful services as well as binary data.
Usage:
http://yourserver.com/proxy.asp?url=<url_encoded_desitnation_url>
[&mimeType=<mimeType>]
Example of using the ASP proxy to download binary data: http://yourserver.com/proxy.asp?url=
http://someserver/1.jpg&mimeType=image/jpg.
Following is an open source script for a server that supports ASP:
FLEX MASHUPS
337
<%
set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
strURL = Request("url")"
objHttp.open "GET", strURL, False
objHttp.Send
If objHttp.status = 200 Then
Response.Expires = 90
Response.ContentType = Request("mimeType")
Response.BinaryWrite objHttp.responseBody
set objHttp = Nothing
End If
%>
The ASP example is pretty straightforward. You set the server URL we are accessing and then we paste
the content on the page.
JSP Proxy
Using JSP technology is similar to ASP. You can set the following proxy to be used for RESTful services
as well as binary data.
The following is an open source script for a server that supports JSP:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.io.BufferedReader,
java.io.InputStreamReader,
java.io.IOException,
java.io.InputStream,
java.net.MalformedURLException,
java.net.URL,
java.net.URLConnection"
private String contentURL;
public static final String CONTENT_URL_NAME = "contentURL";
// get the url through the request:
If (contentURL == null) {
contentURL = (String)request.getAttribute(CONTENT_URL_NAME);
if (contentURL == null) {
contentURL=(String)request.getParameter(CONTENT_URL_NAME);
}
}
if (contentURL == null) {
throw new ServletException("A content URL must be provided,
as a"'" + CONTENT_URL_NAME +
"'" request attribute or request parameter.");
URL url = null;
}
try {

Get AdvancED Flex 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.