2016-04-16 1 views
0

GoogleのAPI APIクライアントから返されるオブジェクトに問題があります。私はそのタグを使って投稿しようとしましたが、助けがありませんでした。私はその後、オブジェクトを配列に変換しましたが、必要なすべてのデータを取得する方法をまだ理解できません。 は基本的に私はGoogleのAPIを使用して検索をやって、それが(使用しますprint_r)は次のようである結果を持つオブジェクトを返します。この多次元配列からデータを取得する方法

Google_Service_Customsearch_Search Object ( 
[collection_key:protected] => promotions 
[internal_gapi_mappings:protected] => Array () 
[contextType:protected] => Google_Service_Customsearch_Context 
[contextDataType:protected] => 
[itemsType:protected] => Google_Service_Customsearch_Result 
[itemsDataType:protected] => 
array [kind] => customsearch#search 
[promotionsType:protected] => Google_Service_Customsearch_Promotion 
[promotionsDataType:protected] => array 
[queriesType:protected] => Google_Service_Customsearch_Query 
[queriesDataType:protected] => map 
[searchInformationType:protected] => Google_Service_Customsearch_SearchSearchInformation 
[searchInformationDataType:protected] => 
[spellingType:protected] => Google_Service_Customsearch_SearchSpelling 
[spellingDataType:protected] => 
[urlType:protected] => Google_Service_Customsearch_SearchUrl 
[urlDataType:protected] => 
[modelData:protected] => Array ( 
    [url] => Array ( 
    [type] => application/json 
    [template] => https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json 
    ) 
    [queries] => Array ( 
     [nextPage] => Array ( 
     [0] => Array ( 
      [title] => Google Custom Search - donkey 
      [totalResults] => 101000000 
      [searchTerms] => donkey 
      [count] => 5 
      [startIndex] => 6 
      [inputEncoding] => utf8 
      [outputEncoding] => utf8 
      [safe] => medium 
      [cx] => 000701869894579992216:cobbhbhbhbhre0 
      [searchType] => image 
      [imgSize] => large) 
     ) 
     [request] => Array ( 
      [0] => Array ( 
      [title] => Google Custom Search - donkey 
      [totalResults] => 101000000 
      [searchTerms] => donkey 
      [count] => 5 
      [startIndex] => 1 
      [inputEncoding] => utf8 
      [outputEncoding] => utf8 
      [safe] => medium 
      .... 
     ) 
     ) 
    ) 
    ) 
    [processed:protected] => Array () 
) 

私はいくつかの項目に値が欠けているので、私は望んでいた値を取得するトラブルを抱えていました最初の1つは[contextDataType:protected] => です。これはあらゆる種類の問題を引き起こします。なぜオブジェクトがこのように返されるのかわかりません。

私は配列に変換されました: $ array =(array)$ results; これは与える:

Array ([*collection_key] => promotions [*internal_gapi_mappings] => Array () [*contextType] => Google_Service_Customsearch_Context [*contextDataType] => [*itemsType] => Google_Service_Customsearch_Result [*itemsDataType] => array [kind] => customsearch#search [*promotionsType] => Google_Service_Customsearch_Promotion [*promotionsDataType] => array [*queriesType] => Google_Service_Customsearch_Query [*queriesDataType] => map [*searchInformationType] => Google_Service_Customsearch_SearchSearchInformation [*searchInformationDataType] => [*spellingType] => Google_Service_Customsearch_SearchSpelling [*spellingDataType] => [*urlType] => Google_Service_Customsearch_SearchUrl [*urlDataType] => [*modelData] => Array ([url] => Array ([type] => application/json [template] => https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json) [queries] => Array ([nextPage] => Array ([0] => Array ([title] => Google Custom Search - donkey [totalResults] => 101000000 [searchTerms] => donkey [count] => 5 [startIndex] => 6 [inputEncoding] => utf8 [outputEncoding] => utf8 [safe] => medium [cx] => 000701869894579992216:coichdjqre0 [searchType] => image [imgSize] => large)) [request] => Array ([0] => Array ([title] => Google Custom Search - donkey [totalResults] => 101000000 [searchTerms] => donkey [count] => 5 [startIndex] => 1 [inputEncoding] => utf8 [outputEncoding] => utf8 [safe] => medium [cx] => 000701869894579992216:coichdjqre0 [searchType] => image [imgSize] => large))) [context] => Array ([title] => Barebones Image Search) [searchInformation] => Array ([searchTime] => 0.241503 [formattedSearchTime] => 0.24 [totalResults] => 101000000 [formattedTotalResults] => 101,000,000) [items] => Array ([0] => Array ([kind] => customsearch#result [title] => Donkey - Wikipedia, the free encyclopedia [htmlTitle] => Donkey - Wikipedia, the free encyclopedia [link] => https://upload.wikimedia.org/wikipedia/commons/7/7b/Donkey_1_arp_750px.jpg [displayLink] => en.wikipedia.org [snippet] => Donkey [htmlSnippet] => Donkey [mime] => image/jpeg [image] => Array ([contextLink] => https://en.wikipedia.org/wiki/Donkey [height] => 536 [width] => 750 [byteSize] => 185637 [thumbnailLink] => https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSQDJFjmFAxjataSZZVvznpqrVKeHzsaVuc8hVxZFRrbjTphJjEsA-qdpA [thumbnailHeight] => 101 [thumbnailWidth] => 141)) [1] => Array ([kind] => customsearch#result [title] => Paradox Resolved: Buridan's Ass [htmlTitle] => Paradox Resolved: Buridan's Ass [link] => http://steve-patterson.com/wp-content/uploads/2014/10/donkey.jpg [displayLink] => steve-patterson.com [snippet] => Paradox Resolved: Buridan's [htmlSnippet] => Paradox Resolved: Buridan's [mime] => image/jpeg [image] => Array ([contextLink] => http://steve-patterson.com/paradox-resolved-buridans-ass/ [height] => 434 [width] => 788 [byteSize] => 138112 [thumbnailLink] => https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSwbhNOJuWaxB11jW9bwmthywOP0-SZictrk0nFOubN_kC3RmFFwuY51lA [thumbnailHeight] => 79 [thumbnailWidth] => 143)) [2] => Array ([kind] => customsearch#result [title] => Donkey (Shrek) - Wikipedia, the free encyclopedia [htmlTitle] => Donkey (Shrek) - Wikipedia, the free encyclopedia [link] => https://upload.wikimedia.org/wikipedia/en/1/14/Donkey_from_Shrek.jpg [displayLink] => en.wikipedia.org [snippet] => Donkey from Shrek.jpg [htmlSnippet] => Donkey from Shrek.jpg [mime] => image/jpeg [image] => Array ([contextLink] => https://en.wikipedia.org/wiki/Donkey_(Shrek) [height] => 500 [width] => 288 [byteSize] => 30244 [thumbnailLink] => https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTWh3l3LYGEt28whI5t1gNNZ0QmZDnEXnFiJjxU4N_nZra_6jKIOGup0cI [thumbnailHeight] => 130 [thumbnailWidth] => 75)) [3] => Array ([kind] => customsearch#result [title] => Donkey Milk [htmlTitle] => Donkey Milk [link] => http://ep.yimg.com/ay/yhst-134322588545647/donkey-milk-1.gif [displayLink] => www.exoticmeatmarkets.com [snippet] => Donkey Milk [htmlSnippet] => Donkey Milk [mime] => image/gif [fileFormat] => Image Document [image] => Array ([contextLink] => http://www.exoticmeatmarkets.com/donkeymilk.html [height] => 600 [width] => 485 [byteSize] => 206399 [thumbnailLink] => https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTQTXK38GlIsFBKZ_EoLHNID74zzK_SxOqpTlwz-9Kkd6obb-ouS0-bycw2 [thumbnailHeight] => 135 [thumbnailWidth] => 109)) [4] => Array ([kind] => customsearch#result [title] => All About DONKEYS! [htmlTitle] => All About DONKEYS! [link] => http://www.lovelongears.com/rabbit.JPG [displayLink] => www.lovelongears.com [snippet] => A small standard donkey, [htmlSnippet] => A small standard donkey, [mime] => image/jpeg [image] => Array ([contextLink] => http://www.lovelongears.com/about_donkeys2.html [height] => 407 [width] => 432 [byteSize] => 22080 [thumbnailLink] => https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRPVrm8Yay1R1-7wevhnpHqLgN9gb0cyR0ZHwLvY99ghu4W8VHbvFxzVA [thumbnailHeight] => 119 [thumbnailWidth] => 126)))) [*processed] => Array ()) 

これは、はるかに使用可能になります、と私はいくつかの値を取得するために、配列をフラット化することができますが、キーが再利用されているので、私はその後、緩いすべての最後の結果けど。具体的には、それぞれの検索結果に1つずつ、[link]の値が5つあります。

どうすればいいですか?私はこれが簡単だと確信していますが、私はそれを理解することはできません。 OK

おかげ

+1

'Google_Service_Customsearch'クラス/ libaryは、そのデータを取得するための方法を提供していませんか? – Jeff

+1

https://api.kdyby.org/class-Google_Service_Customsearch_Search.html – fusion3k

+0

書籍API用の例では、類似のオブジェクトを返しますが、配列のように繰り返してもかまいません。 – bobdonkey

答えて

0

は、私はちょうど配列にそれを作った後、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'); 
関連する問題