disqus / php-disqus

Old PHP bindings for Disqus
http://www.disqus.com
Apache License 2.0
24 stars 12 forks source link

json_decode support for associative arrays #3

Open teastburn opened 13 years ago

teastburn commented 13 years ago

Hi, I needed support for arrays from json_decode for my application (Drupal Disqus module which I changed to use this API instead) so added it locally and wanted to share. Nothing too exciting. Minimally tested but worked for both objects and arrays for me. Here you go:

--- disqus/disqus.php 2010-10-13 16:46:02.000000000 -0700 +++ disqusnew/disqus.php 2010-10-08 12:49:00.000000000 -0700 @@ -38,13 +38,13 @@ define('DISQUS_ACTION_KILL', 'kill');

 if (!extension_loaded('json')) {
    require_once(dirname(__FILE__) . '/json.php');
-   function dsq_json_decode($data, $assoc = false) {
+   function dsq_json_decode($data) {
        $json = new JSON;
-       return $json->unserialize($data, $assoc);
+       return $json->unserialize($data);
    }
 } else {
-   function dsq_json_decode($data, $assoc = false) {
-       return json_decode($data, $assoc);
+   function dsq_json_decode($data) {
+       return json_decode($data);
    }   
 }

@@ -61,7 +61,6 @@ class DisqusAPI {
    var $forum_api_key;
    var $api_url = 'http://www.disqus.com/api/';
    var $api_version = '1.1';
-   var $json_assoc = false;

    /**
     * Creates a new interface to the Disqus API.
@@ -121,11 +120,10 @@ class DisqusAPI {
            return false;
        }

-       $data = dsq_json_decode($response['data'], $this->json_assoc);
-       $data_arr = (array) $data;
+       $data = dsq_json_decode($response['data']);

-       if(!$data_arr || !$data_arr['succeeded']) {
-           $this->last_error = $data_arr['message'];
+       if(!$data || !$data->succeeded) {
+           $this->last_error = $data->message;
            return false;
        }

@@ -134,7 +132,7 @@ class DisqusAPI {
            return false;
        }

-       return !$this->json_assoc ? $data->message : $data['message'];
+       return $data->message;
    }

    /**

teastburn commented 13 years ago

Oops, this is backwards. oh well, plus equals minus, minus equals plus.