[英]How to json_encode PHP object returned by Google Calendar API
[英]Need guidance on how to use PHP to read JSON object returned by Google Custom REST API
谷歌 REST 自定義搜索 API https://developers.google.com/custom-search/v1/using_rest返回一個 JSON 對象(參見下面的部分):
我需要獲取 items->image->contextLink
當我對結果調用“json_decode()”時,我得到 NULL(無法解碼)
使用 PHP 讀取 $result 以獲取內容的適當方法是什么?
我嘗試使用簡單的 while 循環直接讀取結果而不使用 json_dcode,如下所示,但也失敗了。
$i=0;
while($i <= 10)
{
dd($response->items[$i]->image->contextLink);
// API callback
hndlr({
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&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": {
"request": [
{
"title": "Google Custom Search - morgan dollar ms 67",
"totalResults": "421000",
"searchTerms": "morgan dollar ms 67",
"count": 10,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "000858222193663878000:k4vswxxxxxx",
"searchType": "image"
}
],
"nextPage": [
{
"title": "Google Custom Search - morgan dollar ms 67",
"totalResults": "421000",
"searchTerms": "morgan dollar ms 67",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "000858222193663878000:k4vswxxxxxx",
"searchType": "image"
}
]
},
"context": {
"title": "Coin Images"
},
"searchInformation": {
"searchTime": 0.94481,
"formattedSearchTime": "0.94",
"totalResults": "421000",
"formattedTotalResults": "421,000"
},
"items": [
{
"kind": "customsearch#result",
"title": "1880-S MS67 MORGAN SILVER DOLLAR PCGS MS-67 LOOKS CAMEO | eBay",
"htmlTitle": "1880-S \u003cb\u003eMS67 MORGAN SILVER DOLLAR\u003c/b\u003e PCGS \u003cb\u003eMS\u003c/b\u003e-\u003cb\u003e67\u003c/b\u003e LOOKS CAMEO | eBay",
"link": "https://bestcoin.com/ebay/1880-s-morgan-pcgs-ms67.jpg",
"displayLink": "www.ebay.com",
"snippet": "1880-S MS67 MORGAN SILVER DOLLAR PCGS MS-67 LOOKS CAMEO | eBay",
"htmlSnippet": "1880-S \u003cb\u003eMS67 MORGAN SILVER DOLLAR\u003c/b\u003e PCGS \u003cb\u003eMS\u003c/b\u003e-\u003cb\u003e67\u003c/b\u003e LOOKS CAMEO | eBay",
"mime": "image/jpeg",
"fileFormat": "image/jpeg",
"image": {
"contextLink": "https://www.ebay.com/itm/1880-S-MS67-MORGAN-SILVER-DOLLAR-PCGS-MS-67-LOOKS-CAMEO-/350073488185",
"height": 573,
"width": 450,
"byteSize": 51937,
"thumbnailLink": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPA8HHaGrFDACNwFuHiNLgR__uuVnDawzbmzTlLWaD_T0oITQ85YzpVi8p&s",
"thumbnailHeight": 134,
"thumbnailWidth": 105
}
},
我想到了。
我正在使用 &callback=hndlr 調用 REST 調用
取出它允許 json_decode 工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.