0
我對從php php api客戶端返回的對象有些麻煩。我曾嘗試使用該標籤張貼,但沒有幫助。我已經將該對象轉換爲數組,但仍然無法弄清楚如何獲取我需要的所有數據。 基本上我使用谷歌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 ()
)
我有麻煩獲取我想要的,因爲一些物品丟失值的值在這裏,第一個是[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 ())
這看起來更有用,我可以展平陣列得到一些價值,但是由於按鍵被重用,然後我鬆散所有,但最後的結果。具體而言,我想獲得其中有5個,每個搜索結果中有一個的[鏈接]的所有值。
我該怎麼做?我相信這很容易,但我無法弄清楚。
感謝
請問'Google_Service_Customsearch'類/ libary不能提供檢索數據的方法? – Jeff
https://api.kdyby.org/class-Google_Service_Customsearch_Search.html – fusion3k
在他們給出的示例中,它是Books API的示例,它返回一個類似的對象,但是您可以像遍歷數組一樣遍歷它 – bobdonkey