require_once dirname(__FILE__) . '/url.php'; if (!extension_loaded('json')) { require_once dirname(__FILE__) . '/json.php'; function dsq_json_decode($data) { $json = new JSON(); return $json->unserialize($data); } } else { function dsq_json_decode($data) { return json_decode($data); } } global $DISQUS_API_INTERFACES; $DISQUS_API_INTERFACES = dsq_json_decode(file_get_contents(dirname(__FILE__) . '/interfaces.json')); class DisqusInterfaceNotDefined extends Exception { } class DisqusAPIError extends Exception { public function __construct($code, $message) { $this->code = $code; $this->message = $message; } } class DisqusResource { public function __construct($api, $interface = null, $node = null, $tree = array()) {
/** * Makes a call to a Disqus API method. * * @return * The Disqus object. * @param $method * The Disqus API method to call. * @param $args * An associative array of arguments to be passed. * @param $post * TRUE or FALSE, depending on whether we're making a POST call. */ function call($method, $args = array(), $post = false) { $url = $this->api_url . $method . '/'; if (!isset($args['user_api_key'])) { $args['user_api_key'] = $this->user_api_key; } if (!isset($args['forum_api_key'])) { $args['forum_api_key'] = $this->forum_api_key; } if (!isset($args['api_version'])) { $args['api_version'] = $this->api_version; } foreach ($args as $key => $value) { // XXX: Disqus is lacking some exception handling and we sometimes // end up with 500s when passing invalid values if (empty($value)) { unset($args[$key]); } } if (!$post) { $url .= '?' . dsq_get_query_string($args); $args = null; } if (!($response = dsq_urlopen($url, $args)) || !$response['code']) { $this->last_error = 'Unable to connect to the Disqus API servers'; return false; } if ($response['code'] != 200) { if ($response['code'] == 500) { // Try to grab the exception ID for better reporting if (!empty($response['headers']['X-Sentry-ID'])) { $this->last_error = 'DISQUS returned a bad response (HTTP ' . $response['code'] . ', ReferenceID: ' . $response['headers']['X-Sentry-ID'] . ')'; return false; } } elseif ($response['code'] == 400) { $data = dsq_json_decode($response['data']); if ($data && $data->message) { $this->last_error = $data->message; } else { $this->last_error = "DISQUS returned a bad response (HTTP " . $response['code'] . ")"; } return false; } $this->last_error = "DISQUS returned a bad response (HTTP " . $response['code'] . ")"; return false; } $data = dsq_json_decode($response['data']); if (!$data) { $this->last_error = 'No valid JSON content returned from Disqus'; return false; } if (!$data->succeeded) { if (!$data->message) { $this->last_error = '(No error message was received)'; } else { $this->last_error = $data->message; } return false; } $this->last_error = null; return $data->message; }
/** * Makes a call to a Disqus API method. * * @return * The Disqus object. * @param $method * The Disqus API method to call. * @param $args * An associative array of arguments to be passed. * @param $post * TRUE or FALSE, depending on whether we're making a POST call. */ function call($method, $args = array(), $post = false) { $url = $this->api_url . $method . '/'; if (!isset($args['user_api_key'])) { $args['user_api_key'] = $this->user_api_key; } if (!isset($args['forum_api_key'])) { $args['forum_api_key'] = $this->forum_api_key; } if (!isset($args['api_version'])) { $args['api_version'] = $this->api_version; } foreach ($args as $key => $value) { // XXX: Disqus is lacking some exception handling and we sometimes // end up with 500s when passing invalid values if (empty($value)) { unset($args[$key]); } } if (!$post) { $url .= '?' . dsq_get_query_string($args); $args = null; } if (!($response = dsq_urlopen($url, $args)) || !$response['code']) { $this->last_error = 'Unable to connect to the Disqus API servers'; return false; } $data = dsq_json_decode($response['data']); if (!$data || !$data->succeeded) { $this->last_error = $data->message; return false; } if ($response['code'] != 200) { $this->last_error = 'Unknown error'; return false; } return $data->message; }