วันก่อนได้เขียนวิธีการตรวจสอบหมายเลขบัตรประจำตัวประชาชนด้วย web service
ไปแล้ววันนี้มาดูวิธีการเชื่อมต่อ google web service ด้วย php
สิ่งที่เราต้องมีก็คือ Google API developer's key สามารถสมัครขอ key ได้ที่
http://www.google.com/apis/
<!--
# googly.php
# A typical Google Web API php script
# Usage: googly.php?query=<query>
-->
<html>
<head>
<title>googly.php</title>
</head>
<body>
<?
# 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' => 'UTF-8',
'oe' => 'UTF-8'
);
# Create a new SOAP client, feeding it GoogleSearch.wsdl onGoogle's site
$soapclient = new
soapclient('http://api.google.com/GoogleSearch.wsdl',
'wsdl');
# query Google
$results = $soapclient->call('doGoogleSearch',$parameters);
# 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>
parameter ที่เราส่งไปยัง google นั้นมีความหมายดังต่อไปนี้ครับ
query
key word ที่ใช้ในการค้นหา ในที่นี้เราจะรับจาก querystring
start
เป็น offset เริ่มต้นมีค่า defualt เป็น 0
maxResults
เป็นค่าผลตอบรับสูงสุดที่ google ส่งค่ากลับมา
filter
เป็นการกลั่นกรองผลที่ได้รับ เช่น SafeSearch คือการกลั่นกรองผลที่รับให้มีเนื้อหาเหมาะสมกับผู้ค้น
สามารถกำหนดค่าเป็น True หรือ False
restrict
ข้อจำกัดในการค้น เช่น U.S. Government (unclesam), Linux (linux), Macintosh
(mac), and FreeBSD (bsd)
safeSearch
เป็นการกลั่นกรองเนื้อหาที่ เช่น content for adult only
lr
เป็นข้อจำกัดของภาษา
ie
หรือ input encoding เช่น UTF-8
oe
หรือ output encoding
ตัวอย่างการใช้งาน
http://localhost/googly.php?query=test
