PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/DuckDuckGo/API.php

http://github.com/duckduckgo/php5-duckduckgo
PHP | 163 lines | 81 code | 31 blank | 51 comment | 8 complexity | b13b6f6e51df4c900e5d5134b89da281 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @file DuckDuckGo/API.php
  4. * This file provides the base class to interface with the DuckDuckGo API.
  5. * It will also include any necessary classes.
  6. * With this interface, you can currently only perform ZeroClickInfo queries.
  7. * Simple example:
  8. * $api = new DuckDuckGo\API();
  9. * $info = $api->zeroClickQuery('Internet Relay Chat');
  10. * echo $info->definition;
  11. */
  12. namespace DuckDuckGo;
  13. /* Include the necessary classes. */
  14. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'APIResult.php';
  15. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ZeroClickInfo.php';
  16. class API
  17. {
  18. /* General API options and flags. */
  19. /**
  20. * The API base URL. This defaults to api.duckduckgo.com.
  21. */
  22. public $baseURL;
  23. /**
  24. * Whether to use HTTPS or not. This defaults to FALSE (no HTTPS).
  25. */
  26. public $secure;
  27. /**
  28. * Whether to disallow HTML in the result. This defaults to FALSE (don't disallow HTML).
  29. */
  30. public $noHTML;
  31. /**
  32. * Query-specific flags
  33. */
  34. /**
  35. * Whether or not to return Disambiguation (D) results (ZeroClickInfo).
  36. * Defaults to FALSE (allow disambiguation results).
  37. */
  38. public $noDisambiguations;
  39. /* Constructors and internal functions. */
  40. /**
  41. * Constructor.
  42. */
  43. public function __construct()
  44. {
  45. $this->secure = FALSE;
  46. $this->noHTML = FALSE;
  47. $this->noDisambiguations = FALSE;
  48. $this->baseURL = 'api.duckduckgo.com';
  49. }
  50. /**
  51. * Construct an API URL, given a section and an associative options array.
  52. * @param section Which part of the API to call.
  53. * @param options An associative array containing options to pass in key => value form.
  54. * @return The API URL that corresponds with this query, which then can be retrieved in order to get the query results.
  55. */
  56. protected function constructURL($section, $options)
  57. {
  58. $url = '';
  59. if($this->secure) {
  60. $url .= 'https://' . $this->baseURL;
  61. } else {
  62. $url .= 'http://' . $this->baseURL;
  63. }
  64. $url .= $section;
  65. if(count($options) > 0) {
  66. $url .= '?' . \urlencode(\current(\array_keys($options))) . '=' . \urlencode(\array_shift($options));
  67. foreach($options as $name => $value) {
  68. $url .= '&' . \urlencode($name) . '=' . \urlencode($value);
  69. }
  70. }
  71. return $url;
  72. }
  73. /**
  74. * Given an API section and query options, queries the API and returns the raw results.
  75. * @param section Which part of the API to call.
  76. * @param options An associative array containing options to pass in key => value form.
  77. * @return The raw results of the API call.
  78. */
  79. protected function getAPIResult($section, $options)
  80. {
  81. $url = $this->constructURL($section, $options);
  82. if(\extension_loaded('curl')) {
  83. $curl = \curl_init($url);
  84. \curl_setopt($curl, CURLOPT_HEADER, FALSE);
  85. \curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
  86. \curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);
  87. \curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  88. $result = \curl_exec($curl);
  89. \curl_close($curl);
  90. return $result;
  91. }
  92. if(\function_exists('http_get') && \function_exists('http_parse_message')) {
  93. $options = array(
  94. 'redirect' => 0,
  95. );
  96. return \http_parse_message(\http_get($url), $options)->body;
  97. }
  98. if(\ini_get('allow_url_fopen')) {
  99. $context = \stream_context_create(array(
  100. 'http' => array(
  101. 'follow_location' => 0,
  102. )
  103. ));
  104. $handle = \fopen($url, 'r', FALSE, $context);
  105. $result = \stream_get_contents($handle);
  106. \fclose($handle);
  107. return $result;
  108. }
  109. throw new Exception('Could not find suitable method to retrieve API result. Either install the cURL or pear_http extension, or set allow_url_fopen to 1.');
  110. }
  111. /* API functions. */
  112. /**
  113. * Perform a ZeroClickInfo query against the DuckDuckGo API.
  114. * @param query The term to query for.
  115. * @return A ZeroClickInfo object containing the results of the query.
  116. * @see ZeroClickInfo
  117. */
  118. public function zeroClickQuery($query)
  119. {
  120. $result = $this->getAPIResult('/', array(
  121. 'q' => $query,
  122. 'format' => 'json',
  123. 'no_html' => ($this->noHTML ? 1 : 0),
  124. 'no_redirect' => 1,
  125. 'skip_disambig' => ($this->noDisambiguations ? 1 : 0),
  126. ));
  127. if(!$result) {
  128. throw new Exception('Could not retrieve API result.');
  129. }
  130. return new ZeroClickInfo(\json_decode($result, TRUE));
  131. }
  132. }