You are here

function Disqus::call in Disqus 7

Makes a call to a Disqus API method.

Parameters

$method: The Disqus API method to call.

object $arguments: An associative array of arguments to be passed.

$post: TRUE or FALSE, depending on whether we're making a POST call.

Return value

The Disqus object.

13 calls to Disqus::call()
Disqus::create_post in ./disqus.php
Creates a new post.
Disqus::get_forum_api_key in ./disqus.php
Get a forum API key for a specific forum.
Disqus::get_forum_list in ./disqus.php
Returns an array of hashes representing all forums the user owns.
Disqus::get_forum_posts in ./disqus.php
Get a list of comments on a website.
Disqus::get_num_posts in ./disqus.php
Count a number of comments in articles.

... See full list

File

./disqus.php, line 298
Provides the Disqus PHP API.

Class

Disqus
The Disqus PHP API.

Code

function call($function, $arguments = array(), $post = FALSE) {

  // Construct the arguments.
  $args = '';
  if (!isset($arguments['user_api_key'])) {
    $arguments['user_api_key'] = $this->user_api_key;
  }
  if (!isset($arguments['forum_api_key'])) {
    $arguments['forum_api_key'] = $this->forum_api_key;
  }
  if (!isset($arguments['api_version'])) {
    $arguments['api_version'] = $this->api_version;
  }
  foreach ($arguments as $argument => $value) {
    if (!empty($value)) {
      $args .= $argument . '=' . urlencode($value) . '&';
    }
  }

  // Call the Disqus API.
  $ch = curl_init();
  if ($post) {
    curl_setopt($ch, CURLOPT_URL, $this->api_url . $function . '/');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    curl_setopt($ch, CURLOPT_POST, 1);
  }
  else {
    curl_setopt($ch, CURLOPT_URL, $this->api_url . $function . '/?' . $args);
  }
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  $data = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);

  // Check the results for errors (200 is a successful HTTP call).
  if ($info['http_code'] == 200) {
    $disqus = json_decode($data);
    if ($disqus->succeeded) {

      // There weren't any errors, so return the results.
      return isset($disqus->message) ? $disqus->message : NULL;
    }
    else {
      throw new DisqusException(isset($disqus->message) ? $disqus->message : NULL, 0, $info, $disqus);
    }
  }
  else {
    throw new DisqusException('There was an error querying the Disqus API.', $info['http_code'], $info);
  }
}