Skip to content

Commit c0d0adf

Browse files
committed
Initial skeleton
0 parents  commit c0d0adf

File tree

10 files changed

+285
-0
lines changed

10 files changed

+285
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.idea
2+
/node_modules
3+
/bower_components
4+
/vendor
5+
composer.lock

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Changelog

composer.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "jan-drda/laravel-google-custom-search-engine",
3+
"description": "Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version.",
4+
"homepage": "https://github.com/jdrda/laravel-google-custom-search-engine",
5+
"keywords":
6+
[
7+
"search",
8+
"google",
9+
"custom",
10+
"engine",
11+
"free",
12+
"paid",
13+
"laravel"
14+
],
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Jan Drda",
19+
"email": "jdrda@outlook.com",
20+
"role": "Developer"
21+
}
22+
],
23+
"support": {
24+
"email": "jdrda@outlook.com",
25+
"issues": "https://github.com/jdrda/laravel-google-custom-searchengine/issues",
26+
"wiki": "https://github.com/jdrda/laravel-google-custom-searchengine/wiki",
27+
"source": "https://github.com/jdrda/laravel-google-custom-searchengine/archive/master.zip"
28+
},
29+
"require": {
30+
"php": ">=5.4.0",
31+
"illuminate/support": ">=5.0.0"
32+
},
33+
"autoload": {
34+
"psr-0": {
35+
"JDrda\\LaravelGoogleCustomSearchEngine": "src/"
36+
}
37+
},
38+
"config": {
39+
"sort-packages": true
40+
},
41+
"minimum-stability": "dev"
42+
}

license.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Jan Drda
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

readme.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Laravel Google Custom Search Engine
2+
3+
Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version.
4+
5+
##### Brief history
6+
As Swiftype closed free plans, I started to find some alternative without too much coding, but was unsucessfull.
7+
The best I found was [Spatie's Google Search package](https://github.com/spatie/googlesearch) for Google CSE paid version, so I made
8+
some research and develop package similar way, but independent to Google CSE version.
9+
10+
## Installation
11+
1. Install with Composer
12+
13+
```bash
14+
composer require jdrda/laravel-google-custom-search-engine
15+
```
16+
17+
2. Add the service provider to config/app.php
18+
19+
```php
20+
'providers' => [
21+
'...',
22+
'JDrda\LaravelGoogleCustomeSearchEngine\LaravelGoogleCustomeSearchEngineProvider'
23+
];
24+
```
25+
3. Add alias for Facade to config/app.php
26+
```php
27+
'aliases' => [
28+
...
29+
'GoogleCseSearch' => 'JDrda\LaravelGoogleCustomeSearchEngine\Facades\LaravelGoogleCustomeSearchEngineProvider',
30+
...
31+
]
32+
```
33+
34+
4. Publish the config file
35+
```bash
36+
php artisan vendor:publish --provider="JDrda\LaravelGoogleCustomeSearchEngine\LaravelGoogleCustomeSearchEngineProvider"
37+
```
38+
39+
## Fast configuration
40+
41+
## Documentation
42+
Essetial documentation will be at [Github Wiki](https://github.com/jdrda/laravelgooglecsesearch/wiki)
43+
44+
### License
45+
The Olapus is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
46+
47+
## About
48+
I am independent senior software consultant living in the Czech republic in IT business from 1997.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace JDrda\LaravelGoogleCustomSearchEngine\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class GoogleCustomSearchEngine extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'laravelGoogleCustomSearchEngine';
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace JDrda\LaravelGoogleCustomeSearchEngine\Interfaces;
4+
5+
interface LaravelGoogleCustomSearchEngineInterface
6+
{
7+
/**
8+
* Get search results
9+
*
10+
* @param $phrase search phrase
11+
* @param array $parameters all parameters listed at https://developers.google.com/custom-search/json-api/v1/reference/cse/list
12+
* @return mixed
13+
*/
14+
public function getResults($phrase, $parameters = array());
15+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace JDrda\LaravelGoogleCustomSearchEngine;
4+
5+
use Exception;
6+
use JDrda\LaravelGoogleCustomSearchEngine\Interfaces\LaravelGoogleCustomSearchEngineInterface;
7+
8+
class LaravelGoogleCustomSearchEngine implements LaravelGoogleCustomSearchEngineInterface
9+
{
10+
11+
/**
12+
* Custom search engine ID
13+
*
14+
* @var string
15+
*/
16+
protected $engineId;
17+
18+
/**
19+
* Google console API key
20+
*
21+
* @var string
22+
*/
23+
protected $apiKey;
24+
25+
/**
26+
* Constructor
27+
*
28+
* LaravelGoogleCustomSearchEngine constructor.
29+
* @param $engineId
30+
* @param $apiKey
31+
*/
32+
public function __construct($engineId, $apiKey)
33+
{
34+
$this->engineId = $engineId;
35+
$this->apiKey = $apiKey;
36+
}
37+
38+
/**
39+
* Get search results
40+
*
41+
* @param $phrase
42+
* @return array
43+
* @throws Exception
44+
*/
45+
public function getResults($phrase)
46+
{
47+
$searchResults = array();
48+
49+
if ($phrase == '') {
50+
return $searchResults;
51+
}
52+
53+
if ($this->engineId == '') {
54+
throw new \Exception('You must specify a engineId');
55+
}
56+
57+
if ($this->apiKey == '') {
58+
throw new \Exception('You must specify a apiKey');
59+
}
60+
61+
// create a new cURL resource
62+
$ch = curl_init();
63+
64+
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
65+
curl_setopt($ch, CURLOPT_HEADER, 0);
66+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
67+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
68+
69+
$output = curl_exec($ch);
70+
71+
$info = curl_getinfo($ch);
72+
73+
curl_close($ch);
74+
75+
if ($output === false || $info['http_code'] != 200) {
76+
77+
throw new \Exception("No data returned, code [". $info['http_code']. "] - " . curl_error($ch));
78+
79+
}
80+
81+
82+
return $searchResults;
83+
}
84+
85+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace JDrda\LaravelGoogleCustomSearchEngine;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class LaravelGoogleCustomSearchEngineProvider extends ServiceProvider
8+
{
9+
public function boot()
10+
{
11+
$this->publishes([
12+
__DIR__.'/../../config/laravelGoogleCustomSearchEngine.php' => config_path('laravelGoogleCustomSearchEngine.php'),
13+
]);
14+
}
15+
16+
public function register()
17+
{
18+
$this->app->bind('laravelGoogleCustomSearchEngine', function () {
19+
20+
return new LaravelGoogleCustomSearchEngine(config('laravelGoogleCustomSearchEngine.engineId'),
21+
config('laravelGoogleCustomSearchEngine.apiKey'));
22+
});
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
return [
4+
5+
/**
6+
* If you create your engine at https://cse.google.com/cse/ you will find the ID after you click at Settings.
7+
* Just check the URL you have like https://cse.google.com/cse/setup/basic?cx=search_engine_id
8+
* and the string after cx= is your search engine ID
9+
*/
10+
'engineId' => '',
11+
12+
/**
13+
* For generation API key you have to go to https://console.developers.google.com, than
14+
* 1. click on the menu on the right side of the GoogleAPI logo and click on 'Create project'
15+
* 2. enter the name of the new project - it is up to you, you can use 'Google CSE'
16+
* 3. wait until project is created - the indicator is color circle on the top right corner around the bell icon
17+
* 4. API list is shown - search for 'Google Custom Search API' and click on it
18+
* 5. click on 'Enable' icone on the right side of Custom Search API headline
19+
* 6. click on the 'Credentials' on the left menu under the 'Library' section
20+
* 7. click on the 'Create credentials' and choose 'API key'
21+
* 8. your API key is shown, so copy and paste it here
22+
*
23+
* !! It can take some time to API key will be authorized, wait 10 mins before starting to use it
24+
*/
25+
'apiKey' => ''
26+
];

0 commit comments

Comments
 (0)