Google https://github.com/google/google-api-php-clientに問題があります。 私がしたいのは、カスタム検索エンジン(CSE)を使用して検索結果から詳細を得ることだけです。同じことがbing APIを使って30分かかりましたが、これを動作させることはできません。 API呼び出しが機能していますが、返されたオブジェクトから結果を取得できません。 上記のソースの例を使用すると、simple-query.phpは書籍のAPI(便利な例です)を使ってうまく動作し、例を使って検索結果の詳細を返すことができます。Google API PHPクライアント - 検索結果の詳細を取得
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
echo $item['selfLink'],"<br /><br/> \n";
echo $item['etag'],"<br /> \n";
echo $item['volumeInfo']['title'],"<br /> \n";
echo $item['volumeInfo']['authors']['0'],"<br /> \n";
}
これはAPIキーとCSE IDを使用しています。
しかし、CSEを試して使用すると、返される結果オブジェクトを使用できません。簡単な検索の例を変更する:
<?php
include_once "templates/base.php";
echo pageHeader("Custom Image Search");
//API request
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Customsearch.php';
//Create the client
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Enter your API key
// Warn if the API key isn't changed.
if ($apiKey == '<YOUR_API_KEY>') {
echo missingApiKeyWarning();
}
$client->setDeveloperKey($apiKey);
$service = new Google_Service_Customsearch($client);
//Do the query
$query = 'donkey';
$optParams = array(
'imgSize' => 'large',
'searchType' => 'image',
'num' => '5',
'safe' => 'medium',
'cx' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //Added your cx or search engine ID here, see https://cse.google.com/cse/
);
$results = $service->cse->listCse($query, $optParams);
//Iterate over the results
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
echo "Test <br/>";
echo $item['link'], "<br /> \n";
}
これは何も返しません。
$結果にvar_dumpを使用すると、検索結果が返されていることがわかります(例として、ロバの画像を検索しています)。
しかし、本例を使用して、私が行って、6件の結果があることがわかります。
echo "Results count: " .count($results);
しかし、CSEの例でこれを行うことだから、当然、私は何を反復処理しています0を返します。
だから、実際にどのように私は実際には、PHPクライアントとCSEを使用して結果を得ることができる教えてください?
おかげ
「APIに含まれるメソッドを使用する」というよりも、apiドキュメントを指すほうが便利です。 [Google_Service_Customsearch_Result](https://github.com/google/google-api-php-client-services/blob/master/src/Google/Service/Customsearch/Result.php)、[Google_Collection](https ://github.com/google/google-api-php-client/blob/master/src/Google/Collection.php)と[Google_Model](https://github.com/google/google-api-php-クライアント/ blob/master/src/Google/Model.php)に 'getItems()'を見つけさせません。 Google_Collectionは 'Iterator'を実装しているので、これが利用可能であると仮定します。 – doub1ejack