Skip to content

Commit 99c2e47

Browse files
authored
First commit
1 parent f7acce1 commit 99c2e47

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

composer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"require": {
3+
"php": "^7.0",
4+
"guzzlehttp/guzzle": "^6.3"
5+
},
6+
"autoload": {
7+
"psr-4": {
8+
"DuckDuckGo\\": "src/DuckDuckGo/"
9+
}
10+
}
11+
}

config.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
return [
4+
/*
5+
| Whether to use HTTPS or not. This defaults to true for security reasons (use HTTPS).
6+
| Default: true.
7+
| Options: true, false.
8+
*/
9+
"https" => true,
10+
11+
/*
12+
| Use JSON or XML format
13+
| Default: "json".
14+
| Options: "xml", "json".
15+
*/
16+
"format" => "json",
17+
18+
/*
19+
| Whether or not to skip Disambiguation results
20+
| Default: true.
21+
| Options: true, false.
22+
*/
23+
"skip_disambiguations" => false,
24+
25+
/*
26+
| Whether to disallow HTML in the result.
27+
| Default: false.
28+
| Options: true, false.
29+
*/
30+
"html" => false,
31+
];

src/DuckDuckGo/Api.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace DuckDuckGo;
4+
5+
use DuckDuckGo\Misc;
6+
use GuzzleHttp\Client as Http;
7+
use GuzzleHttp\Exception\ClientException;
8+
use GuzzleHttp\Exception\ServerException;
9+
10+
Class Api
11+
{
12+
/**
13+
* Api endpoint url.
14+
*
15+
* @var string
16+
*/
17+
protected $endpoint;
18+
19+
/**
20+
* DuckDuckGo Api constructor.
21+
*/
22+
public function __construct()
23+
{
24+
$this->endpoint = "api.duckduckgo.com/";
25+
}
26+
27+
/**
28+
* Perform a query against the DuckDuckGo API.
29+
*
30+
* @param string $query
31+
* @throws Exception
32+
* @return Json
33+
*/
34+
public function zeroClickQuery(string $query)
35+
{
36+
$url = $this->buildUrl($query);
37+
$http = new Http();
38+
try {
39+
$response = $http->request("GET", $url);
40+
return $response->getBody();
41+
} catch (ClientException $e) {
42+
throw new Exception($e->getResponse()->getBody(), $e->getResponse()->getStatusCode());
43+
} catch (ServerException $e) {
44+
throw new Exception("Could not retrieve API result.", 503);
45+
}
46+
}
47+
48+
/**
49+
* Build a url with config parameters and query.
50+
*
51+
* @param string $query
52+
* @return string $url
53+
*/
54+
public function BuildUrl(string $query)
55+
{
56+
$misc = new Misc();
57+
58+
$parameters = [
59+
"q" => urlencode($query),
60+
"format" => $misc->getConfig("format"),
61+
"no_html" => $misc->getConfig("html"),
62+
"no_redirect" => 1,
63+
"skip_disambig" => $misc->getConfig("disambiguations"),
64+
];
65+
66+
$url = ($misc->getConfig("https"))
67+
? "https://".$this->endpoint."?"
68+
: "https://".$this->endpoint."?";
69+
70+
$url .= http_build_query($parameters);
71+
return $url;
72+
}
73+
}

src/DuckDuckGo/Misc.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace DuckDuckGo;
4+
5+
class Misc
6+
{
7+
/**
8+
* Path of config.php file
9+
*
10+
* @var string
11+
*/
12+
private $configPath;
13+
14+
/**
15+
* DuckDuckGo Misc constructor.
16+
*/
17+
public function __construct()
18+
{
19+
$this->configPath = realpath(__DIR__ . '/../../config.php');
20+
}
21+
22+
/**
23+
* Retrieve information of config.php file based on key parameter.
24+
*
25+
* @param string $key
26+
* @return string|bool
27+
*/
28+
public function getConfig($key)
29+
{
30+
$config = require($this->configPath);
31+
return (isset($config[$key])) ? $config[$key] : false;
32+
}
33+
}

0 commit comments

Comments
 (0)