├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Config.php ├── Http.php ├── Image.php └── ImageSearch.php └── tests ├── GoogleCreds.php.example └── ImageSearchTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /vendor/ 3 | composer.lock 4 | tests/GoogleCreds.php -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Danny Carrillo 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # google-image-search 2 | You're able to query google for images using Google's new Custom Search API. 3 | 4 | ``` php 5 | ImageSearch::config()->apiKey('API Goes Here'); 6 | ImageSearch::config()->cx('CX Goes Here'); 7 | 8 | ImageSearch::search('civic'); // returns array of results 9 | ``` 10 | 11 | ## Requirements 12 | You need to have an API key and a CX string, both can be found by taking a look here: 13 | https://developers.google.com/custom-search/docs/overview 14 | 15 | ## Installation 16 | Use composer to install 17 | 18 | `composer require odannyc/google-image-search` 19 | 20 | ## Usage 21 | Set your API and CX string using the config method: 22 | 23 | ```php 24 | use odannyc\GoogleImageSearch\ImageSearch; 25 | 26 | ImageSearch::config()->apiKey('API Goes Here'); 27 | ImageSearch::config()->cx('CX Goes Here'); 28 | ``` 29 | 30 | Once that's done, use the search method to query a specific string of text 31 | 32 | ```php 33 | ImageSearch::search('civic'); 34 | ``` 35 | 36 | You can also pass in some filters as a second parameter as an array. 37 | 38 | ```php 39 | ImageSearch::search('civic', ['imgSize' => 'large']); 40 | ``` 41 | 42 | The list of filters can be found on Google's official site: 43 | 44 | https://developers.google.com/custom-search/json-api/v1/reference/cse/list 45 | 46 | It'll return an array of results. The images are specifically in the `items` key. 47 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "odannyc/google-image-search", 3 | "description": "You're able to query google for images using Google's new Custom Search API.", 4 | "authors": [ 5 | { 6 | "name": "Danny Carrillo", 7 | "email": "odannycx@gmail.com" 8 | } 9 | ], 10 | "require": { 11 | "guzzlehttp/guzzle": "^6.2" 12 | }, 13 | "require-dev": { 14 | "phpunit/phpunit": "~6.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "odannyc\\GoogleImageSearch\\": "src/", 19 | "odannyc\\tests\\": "tests/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | tests 14 | 15 | 16 | 17 | 18 | src/ 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/Config.php: -------------------------------------------------------------------------------- 1 | 4 | * @package google-image-search 5 | */ 6 | namespace odannyc\GoogleImageSearch; 7 | 8 | /** 9 | * Class Config. Singleton pattern. 10 | * 11 | * @package odannyc\GoogleImageSearch 12 | */ 13 | class Config 14 | { 15 | private static $instance = null; 16 | private $apiKey; 17 | private $cx; 18 | public $baseUrl = 'https://www.googleapis.com/customsearch/v1'; 19 | 20 | /** 21 | * Sets or returns the instance. 22 | * 23 | * @return null 24 | */ 25 | public static function instance() 26 | { 27 | if (is_null(static::$instance)) { 28 | static::$instance = new Config(); 29 | } 30 | return static::$instance; 31 | } 32 | 33 | /** 34 | * Sets or returns the api key. 35 | * 36 | * @param null $key 37 | * @return mixed 38 | */ 39 | public function apiKey($key = null) 40 | { 41 | if (empty($key)) { 42 | return $this->apiKey; 43 | } 44 | 45 | $this->apiKey = $key; 46 | } 47 | 48 | /** 49 | * Sets or returns the cx. 50 | * 51 | * @param null $cx 52 | * @return mixed 53 | */ 54 | public function cx($cx = null) 55 | { 56 | if (empty($cx)) { 57 | return $this->cx; 58 | } 59 | 60 | $this->cx = $cx; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Http.php: -------------------------------------------------------------------------------- 1 | 4 | * @package google-image-search 5 | */ 6 | namespace odannyc\GoogleImageSearch; 7 | 8 | use GuzzleHttp\Client; 9 | 10 | /** 11 | * Class Http. Used for the http query. 12 | * 13 | * @package odannyc\GoogleImageSearch 14 | */ 15 | class Http 16 | { 17 | public $config; 18 | 19 | public function __construct($config) 20 | { 21 | $this->config = $config; 22 | $this->client = new Client(); 23 | } 24 | 25 | public function query($query, $filters) 26 | { 27 | $query = [ 28 | 'q' => $query, 29 | 'cx' => $this->config->cx(), 30 | 'key' =>$this->config->apiKey(), 31 | 'searchType' => 'image' 32 | ]; 33 | 34 | foreach ($filters as $key => $filter) { 35 | $query[$key] = $filter; 36 | } 37 | 38 | return $this->client->get($this->config->baseUrl, ['query' => $query])->getBody(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Image.php: -------------------------------------------------------------------------------- 1 | 4 | * @package google-image-search 5 | */ 6 | namespace odannyc\GoogleImageSearch; 7 | 8 | /** 9 | * Class Image. Used to query google images. 10 | * 11 | * @package odannyc\GoogleImageSearch 12 | */ 13 | class Image 14 | { 15 | /** 16 | * @param $query 17 | * @param array $filters 18 | * @return mixed 19 | */ 20 | public static function search($query, $filters = []) 21 | { 22 | $http = new Http(Config::instance()); 23 | 24 | return json_decode((string) $http->query($query, $filters), true); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/ImageSearch.php: -------------------------------------------------------------------------------- 1 | 4 | * @package google-image-search 5 | */ 6 | namespace odannyc\GoogleImageSearch; 7 | 8 | /** 9 | * Class ImageSearch. Used as the main entry point to search images. 10 | * 11 | * @package odannyc\GoogleImageSearch 12 | */ 13 | class ImageSearch 14 | { 15 | /** 16 | * Used to search for images. 17 | * 18 | * @param $query 19 | * @param $filters 20 | * @return mixed 21 | */ 22 | public static function search($query, $filters = []) 23 | { 24 | return Image::search($query, $filters); 25 | } 26 | 27 | /** 28 | * Used to set or get config values. 29 | * 30 | * @return null 31 | */ 32 | public static function config() 33 | { 34 | return Config::instance(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/GoogleCreds.php.example: -------------------------------------------------------------------------------- 1 | 4 | * @package google-image-search 5 | * @file GoogleCreds.php 6 | */ 7 | namespace odannyc\tests; 8 | 9 | /** 10 | * Class GoogleCreds. Where you put in your credentials for google. 11 | * 12 | * @package odannyc\tests 13 | */ 14 | class GoogleCreds 15 | { 16 | public static $apiKey = 'Api key goes in here'; 17 | public static $cx = 'CX goes in here'; 18 | } 19 | -------------------------------------------------------------------------------- /tests/ImageSearchTest.php: -------------------------------------------------------------------------------- 1 | 4 | * @package google-image-search 5 | */ 6 | namespace odannyc\tests; 7 | use odannyc\GoogleImageSearch\ImageSearch; 8 | 9 | /** 10 | * Class ImageSearchTest. Main test class for image search. 11 | */ 12 | class ImageSearchTest extends \PHPUnit_Framework_TestCase 13 | { 14 | /** 15 | * Just a main image search test. 16 | */ 17 | public function testImageSearch() 18 | { 19 | ImageSearch::config()->apiKey(GoogleCreds::$apiKey); 20 | ImageSearch::config()->cx(GoogleCreds::$cx); 21 | 22 | $this->assertArrayHasKey('items', ImageSearch::search('civic')); 23 | } 24 | } 25 | --------------------------------------------------------------------------------