Welcome, Guest
  • Author Topic: Tutorial: Using PHP remoting with the Google Web Service  (Read 4833 times)

    Flash-db

    • Administrator
    • Systems Administrator
    • *****
    • Posts: 1876
      • View Profile
      • Flash-db.com
    This Tutorial goes over how to use Flash Remoting with AMF (Actionscript message Format), PHP, and and the Google Web Service.  And shows how you can pass an Object directly from a Web Service to a Flash movie.

    This tutorial is closely related to: http://www.flash-db.com/Board/index.php?board=19;action=display;threadid=3190  But a bit more specific.  One thing to remember is that you can use php remoting for any application - not just web services.  In this case it's just easy access to some valuable site content data.

    Also: This tutorial is using Musicman's remoting application, however a newer and more updated application for PHP remoting can be found at: http://amfphp.sourceforge.net - This will be the version to use in the future.  I would recommend signing up for the AMF-PHP mailing list if your interested in following it's development.

    For those of you interested in the AMF format, Follow this link on Flash coders: http://chattyfig.figleaf.com/flashcoders-wiki/index.php?.sol


    On with the tutorial:

    First we need to set a couple things up.  

    Create a new directory on your server, you will then need to place the following 2 files in that directory.

    1:  Nusoap.php, this is a Soap Toolkit for PHP.  Nusoap will handle all Soap based requests for you.  You can download the latest version of Nusoap from here: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/nusoap/lib/nusoap.php  Click the 'view' link then copy, paste, and rename the file nusoap.php.  (you only need nusoap.php and not the other files).

    2:  amfdata.php, this is a PHP class that parse's amf data.  This is used so that native array's (or any other data type) and objects in Flash and PHP can communicate (be used by each interchangable).  You can find the latest version here: http://www.fontimages.org.uk/flash/amfdata_php.html  (Thanks to Musicman for this!)

    3:  Visit: http://www.google.com/apis/  And create a google Account.  You will need the license Key they provide to access the service.  

    Place both of these files (nusoap.php and amfdata.php) in the same directory.

    Now we need to create a Client script on the server that utilize's both of these files.  Lets call this file 'amfSoapClient.php'.  This will be able to handle any request to almost any webservice.

    amfSoapClient.php
    <br><?php<br>// Include the NuSoap Class to handle Soap based requests.<br>include('nusoap.php');<br><br>// Include the AMF (ActionScript Message Formatter).<br>include('amfdata.php');<br><br>// Create a new connection to the AMF parser.<br>$request       = new amfdata();<br><br>// Create a new Soap Client object.  The first paramter of this is the WSDL file location.<br>$soapclient    = new soapclient($request->fnargs[1],'wsdl');<br>      <br>// Call the service.<br>$data       = $soapclient->call($request->fn,$request->fnargs[0]);   <br>            <br>$request->sendresult($data);<br>?><br>

    Place this file 'amfSoapClient.php' in the same directory as the others.

    If your familar with using Nusoap this should look familar - If not try out some of the examples on this site or read some of the other tutorials dealing specifically with nusoap.

    Some important parts to mention about this script:
    $request = new amfdata();  - creates a new instance of amfdata that will be used to convert between actionscript and PHP.

    The following variables:
    $request->fn  - will contain the function name.  This will be a bit more clear when we look at the flash side.
    $request->fnargs[ 0] - contains an array of parameters that we sent from Flash.  These parameters are used as the input parameters for the Soap request.

    $request->fnargs[ 1] - In this case contains the path to the WSDL file.

    Now lets take a look at the Flash side.  This will make things a bit more clear:
    ActionScript
    [script]

    // Set up a new Net Connection to be used with Flash remoting.
    var nc = new NetConnection();
    nc.connect("amfSoapClient.php");

    // This is the on Result handler.  AKA the function that is called when a result is returned from the remote call.
    var googleResults = {};
    googleResults.onResult = function(result) {
          
       _root.output.htmlText = "<font color=\"#ffffff\">results returned:<b> "+result.estimatedTotalResultsCount+"</b> &nbsp;in <b> "+result.searchTime+" </b> seconds</font><br><br>";

       for (var i in result.resultElements) {
          _root.output.htmlText += "<font color=\"#eeeeee\"><u><a href=\""+result.resultElements.URL+"\" target=\"_blank\">"+result.resultElements.title+"</a></u></font> - <font color=\"#999999\">"+result.resultElements.summary+"</font><br><br>";
       }
       
       // set start and end index so we can page later on.
       _root.startindex    = result.startIndex;
       _root.endindex    = result.endIndex;
       _root.totalCount    = result.estimatedTotalResultsCount;
    }

    function doSearch(query, index) {
       
       // This sets up a New Parameters Object - These parameters are sent to the Actual Web Service.
       // Note we can access any web service without modifying anything on the server in this way!
       parameters                = new Object();
       parameters.key           = "Your Google license KEY GOES HERE";   
       parameters.q              = query;
       parameters.start           = index;
       parameters.maxResults        = 10;
       parameters.filter           = false;   
       parameters.restrict        = '';   
       parameters.safeSearch        = false;
       parameters.lr              = '';
       parameters.ie              = '';
       parameters.oe              = '';   
       
       // The WSDL file location of the Web Service to Call.
       // http://api.google.com/GoogleSearch.wsdl
       wsdl    = "http://api.google.com/GoogleSearch.wsdl";   

       // Call the Service.
       // parameter Order ("methodName", "onResultFunction", "ServiceParameters", "WSDL File")
       nc.call('doGoogleSearch', googleResults, parameters, wsdl);
    }

    function searchGoogle() {
       output.htmlText = "<font color=\"#ffffff\">Searching...</font>";
       doSearch(input.text, 0);
    }

    function searchNext() {
       if (_root.endindex < _root.totalCount) {
          output.htmlText = "<font color=\"#ffffff\">Getting next 10 records...</font>";
          // Calls the doSearch Function with the next page of results specified.
          doSearch(input.text, _root.endindex);
       }
    }
    function searchPrevious() {
       if(_root.startindex > 10) {
          output.htmlText = "<font color=\"#ffffff\">Getting previous 10 records...</font>";
          // Calls the doSearch function.
          doSearch(input.text, _root.startindex - 11);
       }
    }[/script]


    And that's it.  The most interesting part of this is realizing that google returns a fairly complex Associative Array - which we then use directly inside of flash.

    Flash-DB

    Flash-db

    • Administrator
    • Systems Administrator
    • *****
    • Posts: 1876
      • View Profile
      • Flash-db.com
    Download: Google Web Service with PHP remoting
    « Reply #1 on: 02/14/03, 01:41 »
    You Can download all of the above files here:

    http://www.flash-db.com/temp2/googleRemoting.zip

    All that you should have to do to get this working is add your Google License Key to the Flash FLA.  Then upload it to any directory on any server that has PHP installed.

    « Last Edit: 02/14/03, 01:46 by Flash-db »
    Flash-DB

    Flash-db

    • Administrator
    • Systems Administrator
    • *****
    • Posts: 1876
      • View Profile
      • Flash-db.com
    Flash-DB

    Navneet Behal

    • Server what's that
    • *
    • Posts: 3
      • View Profile
      • Email
    Good Work  ;)

    Just to show another possible way to do this, there is example that I made a while ago at http://www.productbeta.com/tutorials/show.php?goomba=6& which uses PHP and loadVars. The string which comes from PHP is html formatted on the server and is displayed verbatim in the results field. This allows for a slightly faster approach since PHP has Regular expression support and can alter the data from Google in a faster way.

    Cheers
    Navneet