This may be a dumb question - I haven't used Flash for a very (very) long time. I've been tasked with seeing if it's possible to implement a web service called Postcode Anywhere into a Flash application.
I have php sample code (supplied by the provider) ... looking it over, it appears to simply send a request to their server [containing access keys and the actual postcode]. Their server generates an XML file of results which we read and parse for the addresses... Take a look at the code and tell me if I got this right or wrong for starters.
So the question is, can this php script be emulated in Flash? Will we run into problems with 'urlencode()' or anything else ... does Flash have a matching function? Is that even relevent?
I'm digging from the ground up, as you can see...so any insight is appreciated...
Cheers,
php code:
Code:
<html>
<head>
<title>Postcode Anywhere Address Finder</title>
</head>
<body>
<?php
/*
Note: This script has been developed for .php version 4.2.3.
.php version 4.3 has more options to parse xml properly with slightly modified functions.
This script will let you lookup an address from a postcode.
!!NOTE: Since this example uses the XML service, fields will only be available in the results
if a value is present i.e. they are not null. If this causes problems the Recordset service
can be used instead - just modify the PrepareData function below to strip out the different
characters in the response.
*/
//enter you account code and license key
$ACCOUNTCODE = "AAAAA11111";
$LICENSEKEY = "AA11-AA11-AA11-AA11";
function ByPostcode($SearchPostcode){
global $ACCOUNTCODE,$LICENSEKEY;
/* Build up the URL to send the request to. */
$sURL = "http://services.postcodeanywhere.co.uk/xml.aspx?";
$sURL .= "account_code=" . urlencode($ACCOUNTCODE);
$sURL .= "&license_code=" . urlencode($LICENSEKEY);
$sURL .= "&action=lookup";
$sURL .= "&type=by_postcode";
$sURL .= "&postcode=" . urlencode($SearchPostcode);
PrepareData($sURL);
}
function FetchAddress($AddressID){
global $ACCOUNTCODE, $LICENSEKEY;
/* Build up the URL to request the data from. */
$sURL = "http://services.postcodeanywhere.co.uk/xml.aspx?";
$sURL .= "account_code=" . urlencode($ACCOUNTCODE);
$sURL .= "&license_code=" . urlencode($LICENSEKEY);
$sURL .= "&action=fetch";
$sURL .= "&style=simple";
$sURL .= "&id=" . $AddressID;
PrepareData($sURL);
}
function PrepareData($URL){
global $Data;
/* Open the URL into a file */
$ContentsFetch=file("$URL");
foreach ($ContentsFetch as $line_num => $line) {
if (strpos($line,"<Item ")!=false) { $Contents[]= $line;}
}
for ($i=0;$i<count($Contents);$i++) {
/* Strip out "<Item " and " />" from the XML */
$Contents[$i]=substr($Contents[$i], 6+strpos($Contents[$i],"<Item "));
$Contents[$i]=substr($Contents[$i], 0, strlen($Contents[$i])-4);
$breakapart=explode("\"",$Contents[$i]);
/* Extract field names and values */
for ($x=0;$x<count($breakapart);$x++){
if ($x % 2 == 0){
$k=trim(str_replace("=", "", $breakapart[$x]));
if ($k!='') { $Data[$i][$k]=$breakapart[$x+1]; }
}
}
}
}
?>
<h1>Address Finder</h1>
<form method="post" action="address_finder.php">
<?php
if (isset($_POST['address'])) {
FetchAddress($_POST['address']);
/*
$Data is populated with
id
seq
organisation_name
line1
line2
line3
line4
line5
post_town
county
postcode
mailsort
barcode
is_residential
is_small_organisation
is_large_organisation
*/
if(count($Data)==0){
echo "<br><h3>Sorry, there was an error fetching your address. Please try again.</h3>";
}
else{
/*
We can loop through the records as there is only one returned.
The address can now be stored in a database, but here we'll display it in a form.
*/
echo "<table>\n";
foreach ($Data as $keyd => $data) {
$organisation = $data["organisation_name"];
$line1 = $data["line1"];
$line2 = $data["line2"];
$line3 = $data["line3"];
$line4 = $data["line4"];
$line5 = $data["line5"];
$town = $data["post_town"];
$county = $data["county"];
$postcode = $data["postcode"];
echo "<tr>\n";
echo "<td>Company</td><td><input type=\"text\" name=\"company\" id=\"company\" value=\"$organisation\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Line1</td><td><input type=\"text\" name=\"line1\" id=\"line1\" value=\"$line1\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Line2</td><td><input type=\"text\" name=\"line2\" id=\"line2\" value=\"$line2\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Line3</td><td><input type=\"text\" name=\"line3\" id=\"line3\" value=\"$line3\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Line4</td><td><input type=\"text\" name=\"line4\" id=\"line4\" value=\"$line4\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Line5</td><td><input type=\"text\" name=\"line5\" id=\"line5\" value=\"$line5\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Town</td><td><input type=\"text\" name=\"town\" id=\"town\" value=\"$town\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>County</td><td><input type=\"text\" name=\"county\" id=\"county\" value=\"$county\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Postcode</td><td><input type=\"text\" name=\"postcode\" id=\"postcode\" value=\"$postcode\"></td>\n";
echo "</tr>\n";
}
echo "</table>";
}
}
elseif (isset($_POST['postcode'])) {
$postcode = $_POST['postcode'];
ByPostcode("$postcode");
/*
$Data is populated with
id
seq(uence)
description
*/
if(count($Data)==0){
echo "<br><h3>No results were found for $postcode. Please try another postcode.</h3>";
}
else{
echo "Please select your address: \n";
echo "<select name=\"address\" id=\"address\">\n";
foreach ($Data as $keyd => $data) {
echo "<option value=\"" . $data["id"] . "\">" . trim($data["description"]) . "</option>\n";
}
echo "</select>\n";
echo "<br><input type=submit value=\"Select this address\">";
}
}
else {
echo "<p>Enter a postcode below and click on the 'Find' button to search for your address.</p>\n";
echo "Postcode: <input name=\"postcode\" id=\"postcode\" value=\"\" type=\"text\" size=\"10\">\n";
echo "<input id=\"btnFind\" type=\"submit\" value=\"Find\">\n";
}
?>
</form>
</body>
</html>
there are two ways, i know of, that can solve your problem.
The first one is using LoadVars. this is used by echoing the data through php on a page, and reading it by flash. It is best that you don't send html content to the screen, as there will be problems loading it into flash.
A second way is to use the built-in XML parser. you simple create one of those, and load the contents out of an xml page or just a php page. This way you have to make sure the html you output is a valid xml data structure (all tags must be closed, only 1 root tag, no spaces in the nodenames, etc...)
If i were you, i just stripped the html of your php page, and parse the xml to flash. in flash you use the XML parser to read it, and you output the data in flash, using a css or something.
for help on XML and/or LoadVars check out the livedocs, or search this forum for "LoadVars" or "XML flash"
Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks
Thanks for the reply; I've got it all running ok using the XMLConnector.
I'm not too impressed with Actionscript so far though, and I'm struggling with some real weirdness in many different areas.
For example, in the following script I have a button triggering an XMLConnector called "addressListLookup". The XMLConnector is bound to a List component, which I want hidden when there are less than 2 results.
When I run the script, the connector loads XML data with 71 entries in it, but addressList.length is not updated (a=0), so the List component doesn't appear. Pressing the Load button a second time updates the .length, and the List appears (a=71).
What am I dong wrong?
Code:
this.addressListLookup.URL = sURL;
this.addressListLookup.trigger();
a = addressList.length;
if (a < 2){
this.addressList.visible = false;
this.nextButton.visible = false;
} else {
this.addressList.visible = true;
this.nextButton.visible = true;
}