dou*_*ack 15 php api client-library google-custom-search
我觉得这对我来说是一个愚蠢的错误,但我无法弄清楚如何使用google-api-php-client进行简单的搜索.我的目标是针对我的网站针对谷歌搜索引擎运行简单的关键字查询.
我已经创建了我的api密钥,一个谷歌搜索引擎并下载了api客户端的版本,但是php客户端的google网站似乎没有任何关于如何使用客户端的文档和我唯一的相关示例到目前为止发现了专门搜索谷歌的图书服务.问题是该示例意味着不同的搜索服务具有不同的搜索结果类型,我找不到任何关于如何从a检索结果的文档Google_Service
.
我想我可以设置这样的简单搜索,但我不知道如何实际检索结果.
include_once __DIR__ . '/vendor/autoload.php';
...
public function __construct($searchTerm) {
$client = new Google_Client();
$client->setApplicationName("My_First_Search");
$client->setDeveloperKey(self::GCSE_API_KEY);
$service = new Google_Service($client);
$optParams = array('filter' => $searchTerm);
$results = $service->???
Run Code Online (Sandbox Code Playgroud)
文档必须在那里,但它不在任何明显的地方....
(更新1/21/17:实际上,这些文档对我没什么帮助,但我会把它们留给我们)
我使用phpdoc为google生成api文档apiclient
.我做了一个回购,把phpdoc和libary放在github上.该phpdocs是可浏览这里.
所以希望这会对某人有所帮助.不幸的是,即使使用文档,我也无法解开正确的用法.我还没有为google apiclient-services软件包生成文档,因为它们很大,但我可以在必要时这样做(取决于github页面上的磁盘限制).
dou*_*ack 15
感谢@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);
Run Code Online (Sandbox Code Playgroud)
结果对象实现了Iterator,因此我们可以按如下方式循环它们:
foreach($results->getItems() as $k=>$item){
var_dump($item);
}
Run Code Online (Sandbox Code Playgroud)
但是,为了使用这个库,你必须首先设置一些谷歌的东西.这些内容最终会在Google API客户端库PHP(Beta)网站上提及,但您必须点击并挖掘它们,即使这样,您也会错过下面的最后一个:
这是一个比上面的例子更现实的例子.它仍然非常简单,但总是很高兴能以两种不同的方式解释一些新的解释性注释.
<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
你必须使用 not Google_Service
, butGoogle_Service_Customsearch
$service = new Google_Service_Customsearch($client);
Run Code Online (Sandbox Code Playgroud)
进而:
$results = $service->cse->listCse($searchTerm, $optParams);
Run Code Online (Sandbox Code Playgroud)