Skip to content

Commit ce1ac85

Browse files
committed
Documentation change
1 parent 8047c6b commit ce1ac85

File tree

3 files changed

+202
-17
lines changed

3 files changed

+202
-17
lines changed

readme.md

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
## Laravel Google Custom Search Engine
2-
32
Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version.
43

54
##### Brief history
@@ -8,21 +7,21 @@ The best I found was [Spatie's Google Search package](https://github.com/spatie/
87
some research and develop package similar way, but independent to Google CSE version.
98

109
## Installation
11-
1. Install with Composer
10+
1/ Install with Composer
1211

1312
```bash
1413
composer require jdrda/laravel-google-custom-search-engine
1514
```
1615

17-
2. Add the service provider to config/app.php
16+
2/ Add the service provider to config/app.php
1817

1918
```php
2019
'providers' => [
2120
'...',
2221
'JanDrda\LaravelGoogleCustomeSearchEngine\LaravelGoogleCustomeSearchEngineProvider'
2322
];
2423
```
25-
3. Add alias for Facade to config/app.php
24+
3/ Add alias for Facade to config/app.php
2625
```php
2726
'aliases' => [
2827
...
@@ -31,18 +30,97 @@ composer require jdrda/laravel-google-custom-search-engine
3130
]
3231
```
3332

34-
4. Publish the config file
33+
4/ Publish the config file
3534
```bash
3635
php artisan vendor:publish --provider="JDrda\LaravelGoogleCustomeSearchEngine\LaravelGoogleCustomeSearchEngineProvider"
3736
```
3837

39-
## Fast configuration
38+
## Configuration
39+
40+
### Creating your custom search engine
41+
1. If you create your engine at https://cse.google.com/cse/ you will find the ID after you click at Settings
42+
2. 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
43+
44+
!! Attention !! If you change style of your Custom search engine, the ID can be changed
45+
46+
### Get your API key
47+
1. go to https://console.developers.google.com, than
48+
2. click on the menu on the right side of the GoogleAPI logo and click on 'Create project'
49+
3. enter the name of the new project - it is up to you, you can use 'Google CSE'
50+
4. wait until project is created - the indicator is color circle on the top right corner around the bell icon
51+
5. API list is shown - search for 'Google Custom Search API' and click on it
52+
6. click on 'Enable' icone on the right side of Custom Search API headline
53+
7. click on the 'Credentials' on the left menu under the 'Library' section
54+
8. click on the 'Create credentials' and choose 'API key'
55+
9. your API key is shown, so copy and paste it here
56+
57+
### Save the configuration values
58+
Save search engine ID and api ID in your config/laravelGoogleCustomSearchEngine
59+
60+
## Usage
61+
62+
### Simple usage
63+
Create an object and call the function getResults to get first 10 results
64+
```php
65+
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
66+
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
67+
```
68+
69+
You can also get information about the search like total records and search time
70+
```php
71+
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
72+
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
73+
$info = $fulltext->getSearchInformation(); // get search information
74+
```
75+
76+
### Advanced usage
77+
You can use any parameter supported at Google. List of parameters is here:
78+
https://developers.google.com/custom-search/json-api/v1/reference/cse/list#parameters
79+
80+
E.g. you want to get next 10 results
81+
```php
82+
$parameters = array(
83+
'start' => 10 // start from the 10th results,
84+
'num' => 10 // number of results to get, 10 is maximum and also default value
85+
)
86+
87+
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
88+
$results = $fulltext->getResults('some phrase', $parameters); // get second 10 results for query 'some phrase'
89+
```
90+
91+
You can also get the raw result from Google including other information
92+
Full list of response variables is available here:
93+
https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
94+
```php
95+
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
96+
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
97+
$rawResults = $fulltext->getRawResults(); // get complete response from Google
98+
```
99+
100+
For getting the number of results only use
101+
```php
102+
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
103+
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
104+
$noOfResults = $fulltext->getTotalNumberOfResults(); // get total number of results (it can be less than 10)
105+
```
106+
107+
If you have more engines / more api keys, you can override the config variables with following functions
108+
109+
```php
110+
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
111+
112+
$fulltext->setEngineId('someEngineId'); // sets the engine ID
113+
$fulltext->setApiKey($apiKey); // sets the API key
114+
115+
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
116+
```
40117

41118
## Documentation
42119
Essetial documentation will be at [Github Wiki](https://github.com/jdrda/laravelgooglecsesearch/wiki)
120+
Now is under the development.
43121

44122
### License
45-
The Olapus is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
123+
This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
46124

47125
## About
48126
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 JanDrda\LaravelGoogleCustomSearchEngine\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class LaravelGoogleCustomSearchEngineFacade 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+
}

src/JanDrda/LaravelGoogleCustomSearchEngine/LaravelGoogleCustomSearchEngine.php

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace JanDrda\LaravelGoogleCustomSearchEngine;
44

55
use 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

Comments
 (0)