Google Hacks 2Nd Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Google Hacks 2Nd Edition [Electronic resources] - نسخه متنی

Tara Calishain

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 96. Program Google in PHP

A simple example of programming the Google Web
API with PHP and the NuSOAP module .

PHP (http://www.php.net/), a
recursive acronym
for PHP Hypertext Processing, has seen wide use as the HTML-embedded
scripting language for web development. Add to that the NuSOAP PHP
module for creating and consuming SOAP-based web services
(http://dietrich.ganx4.com/nusoap) and you
have a powerful combination.

This hack illustrates basic use of PHP and NuSOAP in concert to
interact with the Google Web API.


9.13.1. The Code


Save the following code as a plain text file named
googly.php somewhere on your web site where PHP
is able to run. Don't forget to replace
insert key here
with your Google API key.

<!--
# googly.php
# A typical Google Web API php script.
# Usage: Point your browser at googly.php-->
<html>
<head>
<title>googly.php</title>
</head>
<body>
<h1>Googly</h1>
<form method="GET">
Query: <input name="query" value="<? print $HTTP_GET_VARS['query'] ?>">
<input type="submit" name="Search">
</form>
<?
# Run the search only if you're provided a query to work with.
if ($HTTP_GET_VARS['query']) {
# Use the NuSOAP php library.
require_once('nusoap.php');
# Set parameters.
$parameters = array(
'key'=>'insert key here',
'q' => $HTTP_GET_VARS['query'],
'start' => 0,
'maxResults' => 10,
'filter' => false,
'restrict' => '',
'safeSearch' => false,
'lr' => '',
'ie' => 'latin',
'oe' => 'latin'
);
# Create a new SOAP client, feeding it GoogleSearch.wsdl on Google's site.
$soapclient = new soapclient("http://api.google.com/search/beta2");
# Query Google.
$results = $soapclient->call('doGoogleSearch',$parameters, 'urn:GoogleSearch',
'urn:GoogleSearch');
# Results?
if ( is_array($results['resultElements']) ) {
print "<p>Your Google query for '" . $HTTP_GET_VARS['query'] . "' found "
. $results['estimatedTotalResultsCount'] . " results, the top ten of which are:</p>";
foreach ( $results['resultElements'] as $result ) {
print
"<p><a href='" . $result['URL'] . "'>" .
( $result['title'] ? $result['title'] : 'no title' ) .
"</a><br />" . $result['URL'] . "<br />" .
( $result['snippet'] ? $result['snippet'] : 'no snippet' ) .
"</p>";
}
}
# No results.
else {
print "Your Google query for '" . $HTTP_GET_VARS['query'] . "' returned no results";
}
}
?>
</body>
</html>


9.13.2. Running the Hack


Point your web browser at your googly.php, fill
in a query, and click the Search button. Figure 9-1
shows the results of a search for php.


Figure 9-1. Google results by way of googly.php



9.13.3. See Also


An alternate is the

Services_Google package (http://pear.php.net/package/Services_Google),
a PHP 5 interface to the Google API.



/ 209