Created
September 3, 2015 04:47
-
-
Save ykurnia/598fa7b6b926134620ae to your computer and use it in GitHub Desktop.
Contoh aplikasi untuk menggunakan Google API Client untuk melakukan pencarian terhadap keyword tertentu memakai Google API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head><title>Simple Google Search API Example</title></head> | |
<body> | |
<h1>Simple Google Search API Example</h1> | |
<?php | |
// asumsi file ini ada di folder example, folder dengan level yang sama dengan src | |
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); | |
$client = new Google_Client(); | |
$client->setApplicationName("Nama Aplikasi"); | |
$apiKey = "<API_KEY>"; // masukkan API Key | |
// Warn if the API key isn't changed. | |
if (strpos($apiKey, "<") !== false) { | |
echo missingApiKeyWarning(); | |
exit; | |
} | |
$client->setDeveloperKey($apiKey); | |
$service = new Google_Service_Customsearch($client); | |
$arrOptions = array(); | |
$arrOptions['cx'] = '<Search_Engine_ID>'; // masukkan Search Engine ID | |
$q = 'belajar google custom search'; // contoh keyword yang ingin dicari | |
$result = $service->cse->listCse($q,$arrOptions); | |
if (isset($result['items'])) { | |
// print_r($result['items']); | |
// hasil pencarian ada di index "items" | |
foreach($result['items'] AS $i => $resultItem) { | |
// tampilkan beberapa informasi yang bermanfaat | |
echo " | |
<p> | |
No : " .$i. "<br /> | |
Title : ".$resultItem['title']."<br /> | |
HTML Title : ".$resultItem['htmlTitle']."<br /> | |
Display Link : ".$resultItem['displayLink']."<br /> | |
Formatted Link : ".$resultItem['formattedUrl']."<br /> | |
Snippet : ".$resultItem['snippet']."<br /> | |
HTML Snippet : ".$resultItem['htmlSnippet']."<br /> | |
</p> | |
"; | |
} | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment