@@ -9,7 +9,7 @@ namespace serpapi
99
1010const static string HOST = " https://serpapi.com" ;
1111const static string NAME = " serpapi-cpp" ;
12- const static string VERSION = " 1.0 .0" ;
12+ const static string VERSION = " 0.1 .0" ;
1313
1414using namespace rapidjson ;
1515using namespace std ;
@@ -19,103 +19,108 @@ struct GetResponse {
1919 string payload;
2020};
2121
22- Client::Client (map<string, string> parameter)
22+ Client::Client (const map<string, string>& parameter)
2323{
2424 this ->parameter = parameter;
2525}
2626
27+ Client::~Client ()
28+ {
29+ }
30+
2731/* **
2832* Get HTML search results
2933*/
30- string Client::html (map<string, string> parameter)
34+ string Client::html (const map<string, string>& parameter)
3135{
3236 GetResponse gr = Client::get (" /search" , " html" , parameter);
3337 return gr.payload ;
3438}
3539
36- Document Client::search (map<string, string> parameter)
40+ Document Client::search (const map<string, string>& parameter)
3741{
3842 return Client::json (" /search" , parameter);
3943}
4044
41- Document Client::searchArchive ( string id)
45+ Document Client::search_archive ( const string& id)
4246{
4347 return Client::json (" /searches/" + id + " .json" , map<string, string>());
4448}
4549
46- Document Client::account (map<string, string> parameter)
50+ Document Client::account (const map<string, string>& parameter)
4751{
4852 return Client::json (" /account.json" , parameter);
4953}
5054
51- Document Client::location (map<string, string> parameter)
55+ Document Client::location (const map<string, string>& parameter)
5256{
5357 return Client::json (" /locations.json" , parameter);
5458}
5559
56- Document Client::json (string uri, map<string, string> parameter)
60+ Document Client::json (const string& uri, const map<string, string>& parameter)
5761{
5862 GetResponse gr = get (uri, " json" , parameter);
59- const char * json = gr.payload .c_str ();
63+ const char * json_payload = gr.payload .c_str ();
6064 Document d;
61- d.Parse (json);
62- // if(gr.httpCode != 200 && d.HasMember("error")) {
63- // Value entry(d);
64- // entry.AddMember("httpCode", string(gr.httpCode), d.GetAllocator());
65- // entry.AddMember("error", "serpapi.com did not returned any error message", d.GetAllocator());
66- // }
65+ d.Parse (json_payload);
6766 return d;
6867}
6968
70- string encodeUrl (map<string, string> parameter)
69+ string encodeUrl (CURL* curl, const map<string, string>& parameter)
7170{
72- map<string, string> param (parameter);
7371 string s = " " ;
74- map<string,string>::iterator it;
75- for (it = param .begin (); it != param .end (); ++it)
72+ map<string,string>::const_iterator it;
73+ for (it = parameter .begin (); it != parameter .end (); ++it)
7674 {
7775 if (s != " " )
7876 {
7977 s += " &" ;
8078 }
81- // encode each value in case of special character
82- s += it->first + " =" + it->second ;
79+
80+ char * escaped_key = curl_easy_escape (curl, it->first .c_str (), it->first .length ());
81+ char * escaped_value = curl_easy_escape (curl, it->second .c_str (), it->second .length ());
82+
83+ s += string (escaped_key) + " =" + string (escaped_value);
84+
85+ curl_free (escaped_key);
86+ curl_free (escaped_value);
8387 }
8488 return s;
8589}
8690
87- string Client::url (string output, map<string, string> parameter)
91+ string Client::url (CURL* curl, const string& output, const map<string, string>& parameter)
8892{
8993 // encode parameter
90- string url = encodeUrl (parameter);
91- if (size ( this ->parameter ) > 0 ) {
92- url += " &" ;
93- url += encodeUrl (this ->parameter );
94+ string url_str = encodeUrl (curl, parameter);
95+ if (this ->parameter . size ( ) > 0 ) {
96+ if (!url_str. empty ()) url_str += " &" ;
97+ url_str += encodeUrl (curl, this ->parameter );
9498 }
9599 // append output format
96- url += " &output=" + output;
100+ url_str += " &output=" + output;
97101 // append source language
98- url += " &source=" +NAME+" :" +VERSION;
99- // cout << url << endl;
100- return url;
102+ url_str += " &source=" +NAME+" :" +VERSION;
103+ return url_str;
101104}
102105
103- GetResponse Client::get (string uri, string output, map<string, string> parameter)
106+ GetResponse Client::get (const string& uri, const string& output, const map<string, string>& parameter)
104107{
105108 curl_global_init (CURL_GLOBAL_DEFAULT);
106- curl = curl_easy_init ();
107- const string url = HOST + uri + " ?" + this ->url (output, parameter);
108- curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
109+ CURL* curl = curl_easy_init ();
110+ const string url_params = this ->url (curl, output, parameter);
111+ const string full_url = HOST + uri + " ?" + url_params;
112+
113+ curl_easy_setopt (curl, CURLOPT_URL, full_url.c_str ());
109114 curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
110- curl_easy_setopt (curl, CURLOPT_TIMEOUT, timeout);
115+ curl_easy_setopt (curl, CURLOPT_TIMEOUT, ( long ) timeout);
111116 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
112- curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L );
113- curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L );
117+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L );
118+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L );
114119
115120 long httpCode (0 );
116- unique_ptr< string> httpData ( new string ()) ;
121+ string httpData;
117122 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, callback);
118- curl_easy_setopt (curl, CURLOPT_WRITEDATA, httpData. get () );
123+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, & httpData);
119124
120125 // execute search
121126 curl_easy_perform (curl);
@@ -125,7 +130,7 @@ GetResponse Client::get(string uri, string output, map<string, string> parameter
125130 curl_global_cleanup ();
126131 GetResponse gr;
127132 gr.httpCode = httpCode;
128- gr.payload = * httpData. get () ;
133+ gr.payload = httpData;
129134 return gr;
130135}
131136
0 commit comments