2016-03-29 17 views
2

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を使用して結果を得ることができる教えてください?

おかげ

答えて

0

OK、これは私が最終的にこれをしなかった方法ですが、ちょうど配列にそれを作った後、array_walk_recursive使用:

//Making the returned object into an array 
$array = (array) $results; 
function array_print($item, $key) 
{ 
    if($key=='link'){ 
     echo $item ."<br>"; 
    } 
} 

array_walk_recursive($array, 'array_print'); 
1

あなたは結果を取得するためにAPIに含まれるメソッドを使用する必要がありますアイテムの詳細:

+0

「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

関連する問題