From 8047c6be201cd9c723843086d36b5ebbcde030da Mon Sep 17 00:00:00 2001 From: Jan Drda Date: Wed, 7 Dec 2016 17:05:50 +0100 Subject: [PATCH] Initial --- .gitignore | 5 + changelog.md | 1 + composer.json | 42 +++++++ license.md | 21 ++++ readme.md | 48 ++++++++ .../LaravelGoogleCustomSearchEngine.php | 18 +++ ...ravelGoogleCustomSearchEngineInterface.php | 15 +++ .../LaravelGoogleCustomSearchEngine.php | 110 ++++++++++++++++++ ...aravelGoogleCustomSearchEngineProvider.php | 24 ++++ .../laravelGoogleCustomSearchEngine.php | 28 +++++ 10 files changed, 312 insertions(+) create mode 100644 .gitignore create mode 100644 changelog.md create mode 100644 composer.json create mode 100644 license.md create mode 100644 readme.md create mode 100644 src/JanDrda/LaravelGoogleCustomSearchEngine/Facades/LaravelGoogleCustomSearchEngine.php create mode 100644 src/JanDrda/LaravelGoogleCustomSearchEngine/Interfaces/LaravelGoogleCustomSearchEngineInterface.php create mode 100644 src/JanDrda/LaravelGoogleCustomSearchEngine/LaravelGoogleCustomSearchEngine.php create mode 100644 src/JanDrda/LaravelGoogleCustomSearchEngine/LaravelGoogleCustomSearchEngineProvider.php create mode 100644 src/config/laravelGoogleCustomSearchEngine.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d971226 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea +/node_modules +/bower_components +/vendor +composer.lock diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..825c32f --- /dev/null +++ b/changelog.md @@ -0,0 +1 @@ +# Changelog diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4a658db --- /dev/null +++ b/composer.json @@ -0,0 +1,42 @@ +{ + "name": "jan-drda/laravel-google-custom-search-engine", + "description": "Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version.", + "homepage": "https://github.com/jdrda/laravel-google-custom-search-engine", + "keywords": + [ + "search", + "google", + "custom", + "engine", + "free", + "paid", + "laravel" + ], + "license": "MIT", + "authors": [ + { + "name": "Jan Drda", + "email": "jdrda@outlook.com", + "role": "Developer" + } + ], + "support": { + "email": "jdrda@outlook.com", + "issues": "https://github.com/jdrda/laravel-google-custom-searchengine/issues", + "wiki": "https://github.com/jdrda/laravel-google-custom-searchengine/wiki", + "source": "https://github.com/jdrda/laravel-google-custom-searchengine/archive/master.zip" + }, + "require": { + "php": ">=5.4.0", + "illuminate/support": ">=5.0.0" + }, + "autoload": { + "psr-0": { + "JanDrda\\LaravelGoogleCustomSearchEngine": "src/" + } + }, + "config": { + "sort-packages": true + }, + "minimum-stability": "dev" +} diff --git a/license.md b/license.md new file mode 100644 index 0000000..d0f9f7e --- /dev/null +++ b/license.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Jan Drda + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..142e289 --- /dev/null +++ b/readme.md @@ -0,0 +1,48 @@ +## Laravel Google Custom Search Engine + +Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version. + +##### Brief history +As Swiftype closed free plans, I started to find some alternative without too much coding, but was unsucessfull. +The best I found was [Spatie's Google Search package](https://github.com/spatie/googlesearch) for Google CSE paid version, so I made +some research and develop package similar way, but independent to Google CSE version. + +## Installation +1. Install with Composer + +```bash +composer require jdrda/laravel-google-custom-search-engine +``` + +2. Add the service provider to config/app.php + +```php +'providers' => [ + '...', + 'JanDrda\LaravelGoogleCustomeSearchEngine\LaravelGoogleCustomeSearchEngineProvider' +]; +``` +3. Add alias for Facade to config/app.php +```php +'aliases' => [ + ... + 'GoogleCseSearch' => 'JanDrda\LaravelGoogleCustomeSearchEngine\Facades\LaravelGoogleCustomeSearchEngineProvider', + ... +] +``` + +4. Publish the config file +```bash +php artisan vendor:publish --provider="JDrda\LaravelGoogleCustomeSearchEngine\LaravelGoogleCustomeSearchEngineProvider" +``` + +## Fast configuration + +## Documentation +Essetial documentation will be at [Github Wiki](https://github.com/jdrda/laravelgooglecsesearch/wiki) + +### License +The Olapus is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) + +## About +I am independent senior software consultant living in the Czech republic in IT business from 1997. \ No newline at end of file diff --git a/src/JanDrda/LaravelGoogleCustomSearchEngine/Facades/LaravelGoogleCustomSearchEngine.php b/src/JanDrda/LaravelGoogleCustomSearchEngine/Facades/LaravelGoogleCustomSearchEngine.php new file mode 100644 index 0000000..281a10b --- /dev/null +++ b/src/JanDrda/LaravelGoogleCustomSearchEngine/Facades/LaravelGoogleCustomSearchEngine.php @@ -0,0 +1,18 @@ +engineId = $engineId; + $this->apiKey = $apiKey; + } + + /** + * Setter for engineId, overrides the value from config + * + * @param $engineId + */ + public function setEngineId($engineId){ + $this->engineId = $engineId; + } + + /** + * Setter for apiKey, overrides the value from config + * + * @param $engineId + */ + public function setApiKey($apiKey){ + $this->apiKey = $apiKey; + } + + /** + * Get search results + * + * @param $phrase + * @return array + * @throws Exception + */ + public function getResults($phrase) + { + $searchResults = array(); + + if ($phrase == '') { + return $searchResults; + } + + if ($this->engineId == '') { + throw new \Exception('You must specify a engineId'); + } + + if ($this->apiKey == '') { + throw new \Exception('You must specify a apiKey'); + } + + // create a new cURL resource + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + + $output = curl_exec($ch); + + $info = curl_getinfo($ch); + + curl_close($ch); + + if ($output === false || $info['http_code'] != 200) { + + throw new \Exception("No data returned, code [". $info['http_code']. "] - " . curl_error($ch)); + + } + + + return $searchResults; + } + +} diff --git a/src/JanDrda/LaravelGoogleCustomSearchEngine/LaravelGoogleCustomSearchEngineProvider.php b/src/JanDrda/LaravelGoogleCustomSearchEngine/LaravelGoogleCustomSearchEngineProvider.php new file mode 100644 index 0000000..1e741d0 --- /dev/null +++ b/src/JanDrda/LaravelGoogleCustomSearchEngine/LaravelGoogleCustomSearchEngineProvider.php @@ -0,0 +1,24 @@ +publishes([ + __DIR__.'/../../config/laravelGoogleCustomSearchEngine.php' => config_path('laravelGoogleCustomSearchEngine.php'), + ]); + } + + public function register() + { + $this->app->bind('laravelGoogleCustomSearchEngine', function () { + + return new LaravelGoogleCustomSearchEngine(config('laravelGoogleCustomSearchEngine.engineId'), + config('laravelGoogleCustomSearchEngine.apiKey')); + }); + } +} diff --git a/src/config/laravelGoogleCustomSearchEngine.php b/src/config/laravelGoogleCustomSearchEngine.php new file mode 100644 index 0000000..40ca3a9 --- /dev/null +++ b/src/config/laravelGoogleCustomSearchEngine.php @@ -0,0 +1,28 @@ + '009135221033437726143:k204yzkg1k8', + + /** + * For generation API key you have to go to https://console.developers.google.com, than + * 1. click on the menu on the right side of the GoogleAPI logo and click on 'Create project' + * 2. enter the name of the new project - it is up to you, you can use 'Google CSE' + * 3. wait until project is created - the indicator is color circle on the top right corner around the bell icon + * 4. API list is shown - search for 'Google Custom Search API' and click on it + * 5. click on 'Enable' icone on the right side of Custom Search API headline + * 6. click on the 'Credentials' on the left menu under the 'Library' section + * 7. click on the 'Create credentials' and choose 'API key' + * 8. your API key is shown, so copy and paste it here + * + * !! Attention !! It can take some time to API key will be authorized, wait 10 mins before starting to use it + */ + 'apiKey' => 'AIzaSyABzXEPu8_ucfJqAvzrYgyE69hmS7Wssts' +];