Search code examples
google-searchgoogle-search-api

How can I use google's custom search API to do a site search where the site is dynamic


I want to do a site search using google's search API, but the site will be different for every request.

The problem is that google's web search API is deprecated, but if it weren't, I could use it and just send the following request:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:stackoverflow.com&filter=0

And I could parse it as JSON and get the resultCount, which would be the number I'm looking for.

I have been using this deprecated option, but after so many requests, I get a 403, and google reminds me that I'm using a deprecated API and I hit my daily quota. I know google wants developers to start using the custom search API, but it seems like I would have to make a custom search engine for every site that needs to be searched. I want to be able to perform a site search with only one custom search engine, yet the site can be different for each search.

This is very similar to this stack overflow question, How to get Google Indexed pages count via PHP. The question is almost identical, but I'm specifically asking for a nondeprecated solution.


Solution

  • I found a solution that works well enough for my needs.

    Inside the google custom search engine settings, under the "sites to search" section, there is a dropdown that allows you to tell it to search the entire web instead of just the included sites. It reads

    search the entire web but emphasize included sites

    From there, I just deleted all included sites so there were no sites to emphasize. Essentially, the search engine now searches the entire web like the deprecated search API does. This allowed me to pass the query site:site_url and it returned the information I needed.