Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Custom Search JSON API

I'm playing with Google Custom Search API with Custom Search Engine (CSE) via JSON API. I successfully obtained search result, but I'm clueless about how to obtain the nextPageToken.

https://www.googleapis.com/customsearch/v1?key=MY_API_KEY&cx=MY_SEARCH_ENGINE_ID&q=Testing

The JSON response is as follow:

{
 "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?}&cref={cref?}&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": {
  "nextPage": [
   {
    "title": "Google Custom Search - Testing",
    "totalResults": "2900",
    "searchTerms": "Testing",
    "count": 10,
    "startIndex": 11,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "MY_SEARCH_ENGINE_ID"
   }
  ],
  "request": [
   {
    "title": "Google Custom Search - Testing",
    "totalResults": "2900",
    "searchTerms": "Testing",
    "count": 10,
    "startIndex": 1,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "MY_SEARCH_ENGINE_ID"
   }
  ]
 },
 "context": {
  "title": "Test Search Engine"
 },
 "searchInformation": {
  "searchTime": 0.299265,
  "formattedSearchTime": "0.30",
  "totalResults": "2900",
  "formattedTotalResults": "2,900"
 },
 "items": [
  // ... Search Result here
 ]
}

But how to obtain result of next page via nextPageToken ?

like image 549
Raptor Avatar asked Jun 01 '15 03:06

Raptor


1 Answers

Instead of using nextPageToken, there are 2 parameters I can add into the query string to change the page of results:

  • start: the start index of the result, valid values are integers > 0.
  • num: number of results per page, valid values are 1 ~ 10 (max 10 records can be obtained in 1 page)

Therefore to change to page 2, I have to issue:

start=11&num=10

in query string (given that record per page is 10).

Hope this helps.

like image 71
Raptor Avatar answered Oct 19 '22 00:10

Raptor