From Networks & Transactions 1

Assignments: Assignment2Wei

Online version: http://ernie.art.yale.edu/~weizhou/

Wei, your example below has some good aspects but it's also a little more complicated than it needs to be because it assumes PHP 4. PHP 5 has built-in SOAP capabilities. See the example code from Lab 4. -Dan

<?
////////////////////////////////////////////////////////////
// This is example code of how to query the Google API using
// Web Services, SOAP, and PHP.

// For example:
/*

// set the include path to use the new pear stuff
ini_set( 'include_path', '.:/home/user/pearstuff:/usr/local/lib/php');

*/

// Note that Pear::SOAP has several dependencies on other Pear packages,
// which you should also install on your web server.
/////////////////////////////////////////////////////////////

//
// Initialize SOAP web services
//
include("SOAP/Client.php");

$soapclient = new SOAP_Client('http://api.google.com/search/beta2');
$soapoptions = array('namespace' => 'urn:GoogleSearch',
                 'trace' => 0);

////////////////////////////////////////////////////////////
// Calls the Google API and retrieves the estimated number of
// search results for that query into $num.
function do_search( $query, $key, &$num )
{
        global $soapclient;
        global $soapoptions;

        // Note that we pass in an array of parameters into the Google search.
        // The parameters array has to be passed by reference.
        // The parameters are well documented in the developer's kit on the
        // Google site http://www.google.com/apis

        $params = array(
                'key' => $key, // the Developer's key
                'q' => $query, // the search query
                'start' => 0,      // the point in the search results should Google start
                'maxResults' => 1, // the number of search results (max 10)
                'filter' => false, // should the results be filtered?
                'restrict' => '',
                'safeSearch' => false,
                'lr' => '',
                'ie' => '',
                'oe' => ''
        );

        // Here's where we actually call Google using SOAP.
        // doGoogleSearch is the name of the remote procedure call.

        $ret = $soapclient->call('doGoogleSearch', $params,
                                  $soapoptions);

        if (PEAR::isError($ret))
        {
                print("<br>An error #" . $ret->getCode() . " occurred!<br>");
                print(" Error: " . $ret->getMessage() . "<br>\n");
                return false;
        }
        else // We have proper search results
        {
                // Results from the Google search are stored in the object $ret.
                // The following block of code prints
                // out the structure and contents of the object to the screen:
                print("\n<br><pre>");
                print_r( $ret );
                print("</pre><br>\n");

                // in this example, the only thing we need from the search results
                // is the estimatedTotalResultsCount
                $num = $ret->estimatedTotalResultsCount;
        }

        return true;
}

////////////////////////////////////////////////
// Does Google search with retry.
// Retry is useful because sometimes the connection will
// fail for some reason but will succeed when retried.
function search( $query, $key, &$num )
{
        $result = false;
        $max_retries = 5;
        $retry_count = 0;

        while( !$result && $retry_count < $max_retries )
        {
              $result = do_search( $query, $key, $num );   
                if( !$result )
                {
                        print( "Attempt $retry_count failed.<br>\n");
                }       
                $retry_count++;
        }
        if( !$result )
        {
                print("<br>Sorry, connection to Google failed after retrying several times. Please check that the Google Developer's Key you entered was correct. To obtain a developer's key or for more information on the Google API, visit <a href=\"http://www.google.com/apis/\">Google API home page</a>.<br>\n");
        }
        return $result;
}

//////////////////////////////////////////////////////////
// The main part of this script

print("<html>\n<head>\n<title>Google API Example with PHP, SOAP, and Web Services</title>\n</head>\n<body>");

print("
<h1>Google API Example Using PHP</h1>
<p>For more info on the Google API, visit the <a href=\"http://www.google.com/apis\">Google developer's page</a></p>
"
);

if ( $key == "" )
{
        /*
        You get a developer's key when you register to use Google's API.
        A developer's key is a unique string that identifies you to Google.
        You get a maximum of 1000 searches per day using your developer's key.
        */

        $key = 'thisIsNotaRealKeyBQSgnSiZySpQmfd1wqG'; // put your developer's key here.
}

if( $query != "" )
{
        // remove the slashes that are automatically added by PHP before each quotation mark
        $query = stripslashes($query);

        if( search( $query, $key, $num ) )
        {
                print("<h2>Search results</h2>\n<p>The estimated number of results for the search query <i>$query</i> is <b>$num</b>.</p>");
        }
}

//
// print the input form
//
print("<hr><h2>Test out the Google API with PHP, SOAP, and Web Services:</h2><p>
<form method=\"POST\" action=\"apiexample.php\">
  <p>Search Query <input type=\"text\" name=\"query\" size=\"20\"></p>
  <p>Google Developer's Key <input type=\"text\" name=\"key\" size=\"20\"></p>
  <p><input type=\"submit\" value=\"Submit\"><input type=\"reset\" value=\"Reset\"></p>
</form></p>"
);

print("<hr><h2>About</h2><p>This example was created by <a href=\"http://www.sfu.ca/~gpeters\">Geoff Peters</a>, creator of <a href=\"http://www.googleduel.com/\">GoogleDuel</a> and <a href=\"http://www.googleduel.com/googlebusinessmain.php\">GoogleDuel-Ultra</a>. Feel free to re-use or distribute this source code.</p>");

print("<hr><h2>Source Code</h2><br><br>");

highlight_file("apiexample.php");

print("</body>\n
</html>"
);
?>

Retrieved from http://linkedbyair.net/nt1/wiki/Assignments/Assignment2Wei
Page last modified on April 23, 2006, at 06:58 PM