需要指导如何使用PHP读取Google Custom REST API返回的JSON对象。

问题描述 投票:-2回答:1

Googles REST自定义搜索API https:/developers.google.comcustom-searchv1using_rest。 返回一个JSON对象(见下面部分内容)。

我需要在 items->image->contextLink上获取。

当我在结果上调用 "json_decode() "时,我得到NULL(无法解码)

用PHP读取$result来获取内容的合适方法是什么?

我试过不使用json_dcode直接读取结果,用一个简单的while循环,如下图,也失败了。

$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
      }
    },
php json google-custom-search
1个回答
-1
投票

我想明白了。

我是用&callback=hndlr来调用REST调用的。

把它去掉,json_decode就可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.