简体   繁体   English

需要有关如何使用 PHP 读取 Google 自定义 REST API 返回的 JSON 对象的指导

[英]Need guidance on how to use PHP to read JSON object returned by Google Custom REST API

Googles REST Custom Search API https://developers.google.com/custom-search/v1/using_rest returns a JSON object (see part of it below):谷歌 REST 自定义搜索 API https://developers.google.com/custom-search/v1/using_rest返回一个 JSON 对象(参见下面的部分):

I need to get at items->image->contextLink我需要获取 items->image->contextLink

when I call "json_decode()" on the result I get NULL (can't decode)当我对结果调用“json_decode()”时,我得到 NULL(无法解码)

What's the appropriate way to use PHP to read the $result to get at the content?使用 PHP 读取 $result 以获取内容的适当方法是什么?

I've tried to read the result directly without json_dcode with a simple while loop as follows and that fails as well.我尝试使用简单的 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
      }
    },

I figured it out.我想到了。

I was calling the REST call with &callback=hndlr我正在使用 &callback=hndlr 调用 REST 调用

taking it out allowed json_decode to work取出它允许 json_decode 工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM