From 8da8d65d01644671d6a74891a4916701ec3370f6 Mon Sep 17 00:00:00 2001 From: Fl0Cri Date: Mon, 26 Sep 2022 15:18:42 +0200 Subject: [PATCH 1/3] Use cURL instead of October's legacy Http client --- classes/HttpClient.php | 85 ++++++++++++++++++++++++++++++++++++ components/SearchResults.php | 46 +++++++------------ 2 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 classes/HttpClient.php diff --git a/classes/HttpClient.php b/classes/HttpClient.php new file mode 100644 index 0000000..6c4d74e --- /dev/null +++ b/classes/HttpClient.php @@ -0,0 +1,85 @@ +url = $url; + + return $client; + } + + public function header($name, $value) + { + $this->requestHeaders[$name] = $value; + + return $this; + } + + public function setData($data) + { + $this->requestData = $data; + + return $this; + } + + public function send() + { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_HEADER, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + + curl_setopt($curl, CURLOPT_URL, $this->getEncodedUrl()); + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->getEncodedRequestHeaders()); + + $response = $this->responseRawBody = curl_exec($curl); + $this->code = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $this->responseBody = substr($response, $headerSize); + + curl_close($curl); + + return $this; + } + + public function __toString() + { + return (string) $this->responseBody; + } + + private function getEncodedUrl() + { + return $this->url . '?' . http_build_query($this->requestData); + } + + private function getEncodedRequestHeaders() + { + $requestHeaders = []; + + foreach ($this->requestHeaders as $name => $value) { + $requestHeaders[] = "{$name}: {$value}"; + } + + return $requestHeaders; + } +} diff --git a/components/SearchResults.php b/components/SearchResults.php index df5003f..ebd0d72 100644 --- a/components/SearchResults.php +++ b/components/SearchResults.php @@ -1,15 +1,13 @@ error->message); } - elseif ($response->searchInformation->totalResults) + elseif ($totalResults = data_get($response, 'searchInformation.totalResults')) { // set the hard limit to all results and pagination - $totalResponseResults = min($this->maxResults, $response->searchInformation->totalResults); + $totalResponseResults = min($this->maxResults, $totalResults); $this->page['totalResults'] = $totalResponseResults; - $result = new LengthAwarePaginator($response->items, $totalResponseResults, $this->resultPerPage, $this->currentPage); + $result = new LengthAwarePaginator($response->items, $totalResponseResults, $this->resultPerPage, $this->currentPage); $result->setPath($this->page['baseFileName']); $result->appends('q', $this->search); $this->page['results'] = $result; } } - /** - * Calculate the API url call - * - * @return string - */ - private function buildAPIUrl() - { - - $apiKey = $this->property('apikey'); - $cx = $this->property('cx'); - - $start = min($this->maxResults, ($this->currentPage - 1) * $this->resultPerPage + 1); - $params = array('key' => $apiKey, - 'cx' => $cx, - 'start' => $start, - 'q' => $this->search, - 'num' => $this->resultPerPage); - - return self::APIURL . '?' . http_build_query($params); - } - private function buildRequest() { - $http = Http::make($this->buildAPIUrl(), 'GET'); + $http = HttpClient::make(self::APIURL); - if ($this->property('sendReferer')) - { + $http->setData([ + 'key' => $this->property('apikey'), + 'cx' => $this->property('cx'), + 'start' => min($this->maxResults, ($this->currentPage - 1) * $this->resultPerPage + 1), + 'q' => $this->search, + 'num' => $this->resultPerPage, + ]); + + if ($this->property('sendReferer')) { $http->header('Referer', Request::url()); } From 3fbfd5d254ed795dbf3f72d138216ded3f1d8114 Mon Sep 17 00:00:00 2001 From: Fl0Cri Date: Mon, 26 Sep 2022 15:19:08 +0200 Subject: [PATCH 2/3] Update some wrong information in the readme --- readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index bf7e9aa..6ac0066 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ OctoberCMS plugin to add Google's Custom Search Engine (CSE) Search using the Go ### Search engine ID -When calling the API, the user issues requests against an existing instance of a CSE. Therefore, before using this API you will need to create a CSE in the [CSE Google Control Panel](http://cse.google.com/manage/all). Follow the [tutorial](https://developers.google.com/custom-search/docs/tutorial/creatingcse) to learn more about the different configuration options. Once you have setup a CSE, you can find the CSE's ID in the **Setup > Basics > Details** section of the Control Panel for the CSE. +When calling the API, the user issues requests against an existing instance of a CSE. Therefore, before using this API you will need to create a CSE in the [CSE Google Control Panel](https://programmablesearchengine.google.com/controlpanel/all). Follow the [tutorial](https://developers.google.com/custom-search/docs/tutorial/creatingcse) to learn more about the different configuration options. Once you have setup a CSE, you can find the CSE's ID in the **Setup > Basics > Details** section of the Control Panel for the CSE. ### API key @@ -21,9 +21,9 @@ url="/results" layout="default" [searchResults] -apiKey=XXXXXXXXXXXXXXXXXXX +apikey=XXXXXXXXXXXXXXXXXXX cx=1232342342344:xxxxxx -resultsPerPage=20 +resultsPerPage=10 sendReferer=true == {% component 'searchResults' %} @@ -34,7 +34,7 @@ The default template for rendering the results is {% if results %}