感谢@gennadiy把我放在正确的轨道上。如果没有他用来检索结果的建议,我可能会放弃并去寻找另一个图书馆。幸运的是,这几乎是我使用这个库所需要的一切,所以我认为我已经准备好了。->cse->listCse()
简单示例
执行搜索非常简单;它基本上看起来像这样:
include_once __DIR__ . '/vendor/autoload.php';
$GCSE_API_KEY = "nqwkoigrhe893utnih_gibberish_q2ihrgu9qjnr";
$GCSE_SEARCH_ENGINE_ID = "937592689593725455:msi299dkne4de";
$client = new Google_Client();
$client->setApplicationName("My_App");
$client->setDeveloperKey($GCSE_API_KEY);
$service = new Google_Service_Customsearch($client);
$optParams = array("cx"=>self::GCSE_SEARCH_ENGINE_ID);
$results = $service->cse->listCse("lol cats", $optParams);
结果对象实现了迭代器,因此我们可以按如下方式循环访问它们:
foreach($results->getItems() as $k=>$item){
var_dump($item);
}
谷歌先决条件
但是,为了使用此库,您必须先设置一些Google内容。这些东西最终在Google API Client Libraries PHP(Beta)网站上被提及,但你必须点击并挖掘它们,即使这样,你也会错过下面的最后一个:
-
您将需要一个自定义搜索引擎。不要对这样一个事实感到困惑,即互联网上对自定义搜索引擎的大多数引用都是为那些不尝试进行程序搜索的人提供的。你需要一个,它们很容易在这里设置:https://cse.google.com/cse/all
-
您将需要一个谷歌项目。同样,当您知道要去哪里时,设置也很容易:https://console.developers.google.com/apis/dashboard
-
您将需要一个 API 密钥(也称为开发人员密钥)。转到此处并创建一个新密钥(如果还没有):https://console.developers.google.com/apis/credentials
-
您需要为您的项目启用 Google 自定义搜索。此时,您可以向Google查询,但是如果您尚未为项目启用Google自定义搜索,则可能会收到错误响应。转到仪表板,单击蓝色的“启用 API”链接,搜索 Google 自定义搜索并启用它。https://console.developers.google.com/apis/dashboard
一个经过全面评论的示例
这是一个比上面的例子更现实的例子。它仍然非常简单,但是以两种不同的方式解释新的东西并带有许多解释性注释总是很好的。
<?php
include_once __DIR__ . '/vendor/autoload.php';
/**
* Retrieves a simple set of google results for a given plant id.
*/
class GoogleResults implements IteratorAggregate {
// Create one or more API keys at https://console.developers.google.com/apis/credentials
const GCSE_API_KEY = "nqwkoigrhe893utnih_gibberish_q2ihrgu9qjnr";
/* The search engine id is specific to each "custom search engine"
* you have configured at https://cse.google.com/cse/all
* Remember that you must have enabled Custom Search API for the project that
* contains your API Key. You can do this at the following url:
* https://console.developers.google.com/apis/api/customsearch.googleapis.com/overview?project=vegfetch-v01&duration=PT1H
* If you fail to enable the Custom Search API before you try to execute a search
* the exception that is thrown will indicate this. */
const GCSE_SEARCH_ENGINE_ID = "937592689593725455:msi299dkne4de";
// Holds the GoogleService for reuse
private $service;
// Holds the optParam for our search engine id
private $optParamSEID;
/**
* Creates a service object for our Google Custom Search. The API key is
* permiently set, but the search engine id may be changed when performing
* searches in case you want to search across multiple pre-prepared engines.
*
* @param string $appName Optional name for this google search
*/
public function __construct($appName = "My_Search") {
$client = new Google_Client();
// application name is an arbitrary name
$client->setApplicationName($appName);
// the developer key is the API Key for a specific google project
$client->setDeveloperKey(self::GCSE_API_KEY);
// create new service
$this->service = new Google_Service_Customsearch($client);
// You must specify a custom search engine. You can do this either by setting
// the element "cx" to the search engine id, or by setting the element "cref"
// to the public url for that search engine.
//
// For a full list of possible params see https://github.com/google/google-api-php-client-services/blob/master/src/Google/Service/Customsearch/Resource/Cse.php
$this->optParamSEID = array("cx"=>self::GCSE_SEARCH_ENGINE_ID);
}
/**
* A simplistic function to take a search term & search options and return an
* array of results. You may want to
*
* @param string $searchTerm The term you want to search for
* @param array $optParams See: For a full list of possible params see https://github.com/google/google-api-php-client-services/blob/master/src/Google/Service/Customsearch/Resource/Cse.php
* @return array An array of search result items
*/
public function getSearchResults($searchTerm, $optParams = array()){
// return array containing search result items
$items = array();
// Merge our search engine id into the $optParams
// If $optParams already specified a 'cx' element, it will replace our default
$optParams = array_merge($this->optParamSEID, $optParams);
// set search term & params and execute the query
$results = $this->service->cse->listCse($searchTerm, $optParams);
// Since cse inherits from Google_Collections (which implements Iterator)
// we can loop through the results by using `getItems()`
foreach($results->getItems() as $k=>$item){
var_dump($item);
$item[] = $item;
}
return $items;
}
}