| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Integrate search data into your C++ application. This library is the official wrapper for SerpApi.
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.
C++17 and meson are required.
Download and extract the latest release:
curl -sL https://github.com/serpapi/serpapi-cpp/releases/download/v0.5.0/serpapi-0.5.0.tar.xz | tar xJ
cd serpapi-0.5.0
meson setup build
meson compile -C build
sudo meson install -C buildAdd the dependency to your meson.build:
serpapi_dep = dependency('serpapi')Or if you are using it as a subproject:
serpapi_proj = subproject('serpapi')
serpapi_dep = serpapi_proj.get_variable('libserpapi_dep')#include <iostream>
#include <map>
#include <string>
#include <serpapi.hpp>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
int main() {
const char* env_p = std::getenv("SERPAPI_KEY");
if (!env_p) return 1;
std::string apiKey(env_p);
std::map<std::string, std::string> default_parameter;
default_parameter["api_key"] = apiKey;
default_parameter["engine"] = "google";
serpapi::Client client(default_parameter);
std::map<std::string, std::string> parameter;
parameter["q"] = "coffee";
parameter["location"] = "Austin,TX";
rapidjson::Document d = client.search(parameter);
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
std::cout << "\nINFO: " << buffer.GetString() << std::endl;
return 0;
}This example runs a search for "coffee" on Google. It then returns the results as a RapidJSON Document. See the playground to generate your own code, or check out our local examples.
#include <serpapi.hpp>
// serpapi client created with default parameters
std::map<std::string, std::string> default_params = {
{"api_key", "secret_key"},
{"engine", "google"}
};
serpapi::Client client(default_params);
// search query overview (more fields available depending on search engine)
std::map<std::string, std::string> params = {
{"engine", "google"},
{"q", "Coffee"},
{"location", "Portland,Oregon,United States"},
{"device", "desktop"},
{"hl", "en"},
{"gl", "us"}
};
// formatted search results as a RapidJSON Document
rapidjson::Document results = client.search(params);
// raw search engine html as a String
std::string raw_html = client.html(params);
// raw search engine results as Markdown String
std::string raw_markdown = client.markdown(params);std::map<std::string, std::string> default_parameter = {};
serpapi::Client client(default_parameter);
std::map<std::string, std::string> parameter;
parameter["limit"] = "3";
parameter["q"] = "Austin";
rapidjson::Document doc = client.location(parameter);
const rapidjson::Value& list = doc.GetArray();
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
list.Accept(writer);
std::cout << buffer.GetString() << std::endl;It prints the first 3 locations matching Austin (Texas, Texas, Rochester).
NOTE: api_key is not required for this endpoint.
This API allows retrieving previous search results.
serpapi::Client client({{"api_key", "secret_api_key"}, {"engine", "google"}});
rapidjson::Document results = client.search({{"q", "Coffee"}, {"location", "Portland"}});
std::string search_id = results["search_metadata"]["id"].GetString();
// Now let's retrieve the previous search results from the archive.
rapidjson::Document archived_results = client.search_archive(search_id);serpapi::Client client({{"api_key", "secret_api_key"}});
rapidjson::Document account_info = client.account();Search API features non-blocking search using the option: async=true.
#include <serpapi.hpp>
#include <rapidjson/document.h>
#include <iostream>
int main() {
serpapi::Client client({{"api_key", "YOUR_API_KEY"}, {"engine", "google"}});
std::map<std::string, std::string> parameter;
parameter["q"] = "coffee";
parameter["async"] = "true";
rapidjson::Document d = client.search(parameter);
std::string id = d["search_metadata"]["id"].GetString();
// You can later retrieve the results using search_archive
rapidjson::Document results = client.search_archive(id);
return 0;
}C++ versions validated by Github Actions:
Generated with rake coverage (requires SERPAPI_KEY set to exercise the live API tests):
| File | Lines | Exec | Cover |
|---|---|---|---|
| src/callback.cpp | 11 | 8 | 72% |
| src/serpapi.cpp | 97 | 76 | 78% |
| src/serpapi.hpp | 1 | 1 | 100% |
| TOTAL | 109 | 85 | 78% |
classDiagram
Application *-- serpapi
serpapi *-- Client
class Client {
engine String
api_key String
parameter map
search() Document
html() String
markdown() String
location() Document
search_archive() Document
account() Document
}
We use GitHub Actions for continuous integration.
Set your secret API key in your shell before running tests.
export SERPAPI_KEY="your_secret_key"Check code quality using Lint.
rake lintRun tests.
rake testenv SERPAPI_KEY=SERPAPI_KEY docker run --rm -it -e SERPAPI_KEY --workdir /tmp/serpapi -v $PWD:/tmp/serpapi conanio/gcc10 make install_linux reset allhttps://formulae.brew.sh/formula/googletest https://rapidjson.org/md_doc_tutorial.html
| Back | FazBrowse Home | New Git URL |