Google 自定义搜索 API 未返回结果

Google Custom Search API not returning results

在 google 自定义搜索中,我已将 $key 和 $cx 参数正确设置为我的 url

$googleApiUrl = 'https://www.googleapis.com/customsearch/v1?cx=[MYSEARCHENGINEID]&lr=lang_en&q=jeans&key=[MY_API_KEY]';

这个urlreturn这样的数据

{
  "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 - jeans",
        "searchTerms": "jeans",
        "count": 10,
        "startIndex": 1,
        "language": "lang_en",
        "inputEncoding": "utf8",
        "outputEncoding": "utf8",
        "safe": "off",
        "cx": "MYSEARCHENGINE ID",
        "filter": "0",
        "gl": "in",
        "disableCnTwTranslation": "1"
      }
    ]
  },
  "searchInformation": {
    "searchTime": 0.436263,
    "formattedSearchTime": "0.44",
    "totalResults": "0",
    "formattedTotalResults": "0"
  }
}

我不知道如何在搜索信息中从中获取数据总结果= 0。

你的 return-data 是一个 json 字符串。使用 php 函数 json_decode 您可以轻松地将 json 字符串解码为数组:

$array = json_decode($return_data, TRUE);
echo $array['searchInformation']['totalResults']; // 0

我为 CSE 中的搜索添加了索引 URL。现在它的工作和获取结果。