From 99c2e47f727c034c0f92e28c204549445a44db00 Mon Sep 17 00:00:00 2001 From: Emerson Santiago Date: Tue, 22 Aug 2017 18:43:46 -0300 Subject: [PATCH] First commit --- composer.json | 11 +++++++ config.php | 31 +++++++++++++++++ src/DuckDuckGo/Api.php | 73 +++++++++++++++++++++++++++++++++++++++++ src/DuckDuckGo/Misc.php | 33 +++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 composer.json create mode 100644 config.php create mode 100644 src/DuckDuckGo/Api.php create mode 100644 src/DuckDuckGo/Misc.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..acb00d0 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "require": { + "php": "^7.0", + "guzzlehttp/guzzle": "^6.3" + }, + "autoload": { + "psr-4": { + "DuckDuckGo\\": "src/DuckDuckGo/" + } + } +} diff --git a/config.php b/config.php new file mode 100644 index 0000000..434d383 --- /dev/null +++ b/config.php @@ -0,0 +1,31 @@ + true, + + /* + | Use JSON or XML format + | Default: "json". + | Options: "xml", "json". + */ + "format" => "json", + + /* + | Whether or not to skip Disambiguation results + | Default: true. + | Options: true, false. + */ + "skip_disambiguations" => false, + + /* + | Whether to disallow HTML in the result. + | Default: false. + | Options: true, false. + */ + "html" => false, +]; \ No newline at end of file diff --git a/src/DuckDuckGo/Api.php b/src/DuckDuckGo/Api.php new file mode 100644 index 0000000..bed29c4 --- /dev/null +++ b/src/DuckDuckGo/Api.php @@ -0,0 +1,73 @@ +endpoint = "api.duckduckgo.com/"; + } + + /** + * Perform a query against the DuckDuckGo API. + * + * @param string $query + * @throws Exception + * @return Json + */ + public function zeroClickQuery(string $query) + { + $url = $this->buildUrl($query); + $http = new Http(); + try { + $response = $http->request("GET", $url); + return $response->getBody(); + } catch (ClientException $e) { + throw new Exception($e->getResponse()->getBody(), $e->getResponse()->getStatusCode()); + } catch (ServerException $e) { + throw new Exception("Could not retrieve API result.", 503); + } + } + + /** + * Build a url with config parameters and query. + * + * @param string $query + * @return string $url + */ + public function BuildUrl(string $query) + { + $misc = new Misc(); + + $parameters = [ + "q" => urlencode($query), + "format" => $misc->getConfig("format"), + "no_html" => $misc->getConfig("html"), + "no_redirect" => 1, + "skip_disambig" => $misc->getConfig("disambiguations"), + ]; + + $url = ($misc->getConfig("https")) + ? "https://".$this->endpoint."?" + : "https://".$this->endpoint."?"; + + $url .= http_build_query($parameters); + return $url; + } +} diff --git a/src/DuckDuckGo/Misc.php b/src/DuckDuckGo/Misc.php new file mode 100644 index 0000000..3b296d7 --- /dev/null +++ b/src/DuckDuckGo/Misc.php @@ -0,0 +1,33 @@ +configPath = realpath(__DIR__ . '/../../config.php'); + } + + /** + * Retrieve information of config.php file based on key parameter. + * + * @param string $key + * @return string|bool + */ + public function getConfig($key) + { + $config = require($this->configPath); + return (isset($config[$key])) ? $config[$key] : false; + } +} \ No newline at end of file