Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonsm authored Aug 22, 2017
1 parent f7acce1 commit 99c2e47
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
11 changes: 11 additions & 0 deletions composer.json
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/"
}
}
}
31 changes: 31 additions & 0 deletions config.php
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,
];
73 changes: 73 additions & 0 deletions src/DuckDuckGo/Api.php
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;
}
}
33 changes: 33 additions & 0 deletions src/DuckDuckGo/Misc.php
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;
}
}

0 comments on commit 99c2e47

Please sign in to comment.