33namespace JanDrda \LaravelGoogleCustomSearchEngine ;
44
55use Exception ;
6- use JDrda \ LaravelGoogleCustomSearchEngine \Interfaces \LaravelGoogleCustomSearchEngineInterface ;
6+ use JanDrda \ LaravelGoogleCustomeSearchEngine \Interfaces \LaravelGoogleCustomSearchEngineInterface ;
77
8- class LaravelGoogleCustomSearchEngine implements LaravelGoogleCustomSearchEngineInterface
8+ class LaravelGoogleCustomSearchEngine
99{
1010
1111 /**
@@ -36,10 +36,10 @@ class LaravelGoogleCustomSearchEngine implements LaravelGoogleCustomSearchEngine
3636 * @param $engineId
3737 * @param $apiKey
3838 */
39- public function __construct ($ engineId , $ apiKey )
39+ public function __construct ()
4040 {
41- $ this ->engineId = $ engineId ;
42- $ this ->apiKey = $ apiKey ;
41+ $ this ->engineId = config ( ' laravelGoogleCustomSearchEngine. engineId' ) ;
42+ $ this ->apiKey = config ( ' laravelGoogleCustomSearchEngine. apiKey' ) ;
4343 }
4444
4545 /**
@@ -63,14 +63,31 @@ public function setApiKey($apiKey){
6363 /**
6464 * Get search results
6565 *
66+ * Gets results from CSE only as array of objects where the most important variables are
67+ * [title] - page title
68+ * [htmlTitle] - page title with HTML tags
69+ * [link] - page URL
70+ * [displayLink] - displayed part of page URL
71+ * [snippet] - short text from page with the searched phrase
72+ * [htmlSnippet] - short text from page with the searched phrase with HTML tags
73+ * complete list of parameters with description is located at
74+ * https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
75+ *
76+ * Input parameters are available here
77+ * https://developers.google.com/custom-search/json-api/v1/reference/cse/list#parameters
78+ *
6679 * @param $phrase
6780 * @return array
6881 * @throws Exception
82+ * @link https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
83+ * @link https://developers.google.com/custom-search/json-api/v1/reference/cse/list#parameters
6984 */
70- public function getResults ($ phrase )
85+ public function getResults ($ phrase, $ parameters = array () )
7186 {
72- $ searchResults = array ();
7387
88+ /**
89+ * Check required parameters
90+ */
7491 if ($ phrase == '' ) {
7592 return $ searchResults ;
7693 }
@@ -83,28 +100,100 @@ public function getResults($phrase)
83100 throw new \Exception ('You must specify a apiKey ' );
84101 }
85102
86- // create a new cURL resource
103+ /**
104+ * Create search aray
105+ */
106+ $ searchArray = http_build_query (array_merge (
107+ ['key ' => $ this ->apiKey ],
108+ ['q ' => $ phrase ],
109+ $ parameters
110+ ));
111+
112+ /**
113+ * Add unencoded search engine id
114+ */
115+ $ searchArray = '?cx= ' . $ this ->engineId . '& ' . $ searchArray ;
116+
117+ /**
118+ * Prepare CUrl and get result
119+ */
87120 $ ch = curl_init ();
88121
89- curl_setopt ($ ch , CURLOPT_URL , "http ://www.example .com/" );
122+ curl_setopt ($ ch , CURLOPT_URL , "https ://www.googleapis .com/customsearch/v1 " . $ searchArray );
90123 curl_setopt ($ ch , CURLOPT_HEADER , 0 );
124+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
91125 curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 0 );
92126 curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
127+ curl_setopt ($ ch , CURLOPT_POST , 0 );
93128
94129 $ output = curl_exec ($ ch );
95130
96131 $ info = curl_getinfo ($ ch );
97132
98133 curl_close ($ ch );
99134
135+ /**
136+ * Check HTTP code of the result
137+ */
100138 if ($ output === false || $ info ['http_code ' ] != 200 ) {
101139
102140 throw new \Exception ("No data returned, code [ " . $ info ['http_code ' ]. "] - " . curl_error ($ ch ));
141+ }
142+
143+ /**
144+ * Convert JSON format to object and save
145+ */
146+ $ this ->originalResponse = json_decode ($ output );
103147
148+ /**
149+ * If there are some results, return them, otherwise return blank array
150+ */
151+ if (isset ($ this ->originalResponse ->items )){
152+ return $ this ->originalResponse ->items ;
153+ }
154+ else {
155+ return array ();
104156 }
105157
158+ }
106159
107- return $ searchResults ;
160+ /**
161+ * Get full original response
162+ *
163+ * Gets full originated response converted from JSON to StdClass
164+ * Full list of parameters is located at
165+ * complete list of parameters with description is located at
166+ * https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
167+ *
168+ * @return \stdClass
169+ * @url https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
170+ */
171+ public function getRawResult (){
172+ return $ this ->originalResponse ;
173+ }
174+
175+ /**
176+ * Get search information
177+ *
178+ * Gets basic information about search
179+ * [searchTime] - time costs of the search
180+ * [formattedSearchTime] - time costs of the search formatted according to locale style
181+ * [totalResults] - number of results
182+ * [formattedTotalResults] - number of results formatted according to locale style
183+ *
184+ * @return \stdClass
185+ */
186+ public function getSearchInformation (){
187+ return $ this ->originalResponse ->searchInformation ;
188+ }
189+
190+ /**
191+ * Get the total number of pages where the search phrase is located
192+ *
193+ * @return integer
194+ */
195+ public function getTotalNumberOfpages (){
196+ return $ this ->originalResponse ->searchInformation ->totalResults ;
108197 }
109198
110199}
0 commit comments