phpjsongoogle-search-api

Google Custom Search API not returning results


In google custom search I have set $key and $cx parameter correctly to my url

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

this url return data like this

{
  "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"
  }
}

I do not know how to fetch data from this in search information total result = 0.


Solution

  • your return-data is a json string. with php function json_decode you can easy decode the json string to an array:

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