Home
HomeFlexible, Lightweight and Truly Skinnable Flash ComponentsPHPObject and other Flash ExtensionsForumsBlog JournalLinks
Buy Flash Components
  PHPObject - Opensource PHP Flash Remoting

redhand Using GoogleWebAPI with PHPObject



Author
GhostWire Studios


Overview
This is a tutorial to demonstrate how you can create a Macromedia Flash application that searches the Google index of 3 billion pages, using the Google Web APIs service, and then formats and displays the search results.

We are using PHPObject in this tutorial to make things easier (compare). The following are things you need to complete this tutorial:


In this tutorial, It is assumed that you already know how to create a user-interface in Flash and I won't elaborate on that.


Step One (Installing PHPObject):
We assume you already have the PHPObject library installed. Read Installing PHPObject if you have not done so. You should also read the documentation on Using PHPObject (Web Services).


Step Two (Create the Flash MX application user interface):
You can create your own look-and-feel. Choose how you want the interface to look like. Here is a simple interface:



Step Three (Code the Flash application):
The following shows the actionscript you should enter in the actions panel:

////////////////////////////////////////
// import the library
////////////////////////////////////////
#include"PHPObject.as"

////////////////////////////////////////
// prepare the movie to use the gateway
////////////////////////////////////////
_global.defaultGatewayKey ="secret";
_global.defaultGatewayUrl ="http://mydomain/services/Gateway.php";

////////////////////////////////////////
// create a PHPObject and link it with the web service
////////////////////////////////////////
mySvc =new PHPObject("http://api.google.com/GoogleSearch.wsdl");
mySvc.utf8encode(false);// this method added in v1.4 allows switching off utf8 encoding by gateway, necessary when the results from web service already encoded

////////////////////////////////////////
// set up the onResult responder
////////////////////////////////////////
// the onResult catch the search results returned from Google
// you should define here how you want to parse the data, which comes in as an object
// this is just an example, there are many other ways to present the search results
mySvc.onResult =function(result)
{
output ="";
for (i inresult['resultElements']) {
output +="< p>< a href='"
+result['resultElements'][i]['URL']// here is the link
+"'>< font color='#990000'>< u>"
+ ((result['resultElements'][i]['title'].length>0) ?// title exists?
result['resultElements'][i]['title'] :// use the title
result['resultElements'][i]['URL'])// else use the URL as title
+"< /u>< /font>< /a>< br>"
+result['resultElements'][i]['snippet']// here are words about the site
+"< /p>< br>";
}
results_txt.htmlText = output;
}

////////////////////////////////////////
// set up a function to invoke web service

////////////////////////////////////////
function doSearch(offset) {
results_txt.htmlText ="< b>Processing request... please wait< /b>";// provide feedback to user
// please refer to Google API reference for details on how to configure your search options
mySvc.doGoogleSearch(
"key","licensekey",// enter your license key
"q",query_txt.text,// the query terms, which we get from the input field
"start",offset, // Zero-based index of the first desired result
"maxResults",10,// Number of results desired per query; max 10
"filter",false,// similar results filter
"restrict","",// country or topic filter
"safeSearch",false,// adult content filter
"lr",""// language restrict
);
}

////////////////////////////////////////
// set up button triggers

////////////////////////////////////////
previous_btn.onRelease =function()
{
offset = Math.max(0,offset-10);// zero is minimum
doSearch(offset);
}

next_btn.onRelease =function()
{
offset += 10;
doSearch(offset);
}

search_btn.onRelease =function()
{
offset = 0;
doSearch(offset);
}

////////////////////////////////////////
// initialize variables

////////////////////////////////////////
offset = 0;

////////////////////////////////////////
// make buttons nicer (optional)

////////////////////////////////////////
globalStyleFormat.scrollTrack = 0xBCC4D3;
globalStyleFormat.arrow = 0x313A4A;
globalStyleFormat.face = 0xBCC4D3;
globalStyleFormat.highlight = 0xEEEEEE;
globalStyleFormat.shadow = 0x848284;
globalStyleFormat.darkshadow = 0x000000;
globalStyleFormat.highlight3D = 0xBCC4D3;
globalStyleFormat.applyChanges();

There you go! You now have a simple Google search tool built in Flash. Please refer to the Google Web API Reference for more options on configuring your search options and more details on the results that are returned from the web service. You can also try out the Google spellchecking web service, and of course the many other web services out there.

Have fun!


About PHPObject

PHPObject is an opensource alternative to Flash Remoting for PHP developers. With PHPObject, you can call a method of a PHP class/library on your web server as if the class/library was defined in Flash itself. It takes care of your client-server connections and makes passing of variables (properties) between Flash MX and PHP easy, and thereby providing a convenient way to connect rich media clients with data and business logic residing on your server.

PHPObject is developed by GhostWire Studios and released under the GNU LGPL.


>> Documentation <<

 
Contact UsSite Map
Copyright © 2003-2006 GhostWire Studios