-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"require": { | ||
"php": "^7.0", | ||
"guzzlehttp/guzzle": "^6.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DuckDuckGo\\": "src/DuckDuckGo/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
return [ | ||
/* | ||
| Whether to use HTTPS or not. This defaults to true for security reasons (use HTTPS). | ||
| Default: true. | ||
| Options: true, false. | ||
*/ | ||
"https" => 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, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace DuckDuckGo; | ||
|
||
use DuckDuckGo\Misc; | ||
use GuzzleHttp\Client as Http; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\ServerException; | ||
|
||
Class Api | ||
{ | ||
/** | ||
* Api endpoint url. | ||
* | ||
* @var string | ||
*/ | ||
protected $endpoint; | ||
|
||
/** | ||
* DuckDuckGo Api constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace DuckDuckGo; | ||
|
||
class Misc | ||
{ | ||
/** | ||
* Path of config.php file | ||
* | ||
* @var string | ||
*/ | ||
private $configPath; | ||
|
||
/** | ||
* DuckDuckGo Misc constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->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; | ||
} | ||
} |