Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrda committed Dec 7, 2016
0 parents commit 8047c6b
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea
/node_modules
/bower_components
/vendor
composer.lock
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
21 changes: 21 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -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.
48 changes: 48 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace JanDrda\LaravelGoogleCustomSearchEngine\Facades;

use Illuminate\Support\Facades\Facade;

class GoogleCustomSearchEngine extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'laravelGoogleCustomSearchEngine';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace JanDrda\LaravelGoogleCustomeSearchEngine\Interfaces;

interface LaravelGoogleCustomSearchEngineInterface
{
/**
* Get search results
*
* @param $phrase search phrase
* @param array $parameters all parameters listed at https://developers.google.com/custom-search/json-api/v1/reference/cse/list
* @return mixed
*/
public function getResults($phrase, $parameters = array());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace JanDrda\LaravelGoogleCustomSearchEngine;

use Exception;
use JDrda\LaravelGoogleCustomSearchEngine\Interfaces\LaravelGoogleCustomSearchEngineInterface;

class LaravelGoogleCustomSearchEngine implements LaravelGoogleCustomSearchEngineInterface
{

/**
* Custom search engine ID
*
* @var string
*/
protected $engineId;

/**
* Google console API key
*
* @var string
*/
protected $apiKey;

/**
* Original response converted to array
*
* @var array
*/
protected $originalResponse;

/**
* Constructor
*
* LaravelGoogleCustomSearchEngine constructor.
* @param $engineId
* @param $apiKey
*/
public function __construct($engineId, $apiKey)
{
$this->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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace JanDrda\LaravelGoogleCustomSearchEngine;

use Illuminate\Support\ServiceProvider;

class LaravelGoogleCustomSearchEngineProvider extends ServiceProvider
{
public function boot()
{
$this->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'));
});
}
}
28 changes: 28 additions & 0 deletions src/config/laravelGoogleCustomSearchEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

return [

/**
* If you create your engine at https://cse.google.com/cse/ you will find the ID after you click at Settings.
* Just check the URL you have like https://cse.google.com/cse/setup/basic?cx=search_engine_id
* and the string after cx= is your search engine ID
*
* !! Attention !! If you change style of your Custom search engine, the ID can be changed
*/
'engineId' => '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'
];

0 comments on commit 8047c6b

Please sign in to comment.