Commits

Anonymous committed b921fb6

Updated php client to only use the json version of the api.

Comments (0)

Files changed (2)

php/Repustate.php

 //  Repustate PHP API client.
 //
 //  Requirements:
-//  - PHP >= 5.3, with cURL, JSON and SimpleXML
+//  - PHP >= 5.3, with cURL and JSON
 //
 //  Want to change it / improve it / share it? Go for it.
 //
 
 class Repustate
 {
-    const urlTemplate = 'http://api.repustate.com/%(version)s/%(key)s/%(function)s.%(responseType)s';
+    const urlTemplate = 'http://api.repustate.com/%(version)s/%(key)s/%(function)s.json';
 
     const pptUrl = 'http://api.repustate.com/%(version)s/%(key)s/powerpoint/';
 
-    protected $validResponseTypes = array('json', 'xml');
-
     public $apiKey;
     public $version;
 
     }
 
     //
-    // Retrieve the url for the given api function and response type.
+    // Retrieve the url for the given api function.
     //
-    protected function getUrlForCall($apiFunction, $responseType)
+    protected function getUrlForCall($apiFunction)
     {
         if ($apiFunction == 'powerpoint')
         {
         }
 
         $urlArgs = array(
-            'responseType' => $responseType,
             'function' => $apiFunction,
             'key' => $this->apiKey,
             'version' => $this->version,
         return $result;
     }
 
-    protected function parseResult($result, $apiFunction, $responseType)
+    protected function parseResult($result, $apiFunction)
     {
         if ($apiFunction == 'powerpoint')
         {
             return $result;
         }
-        else if ($responseType == 'json')
-        {
-            return json_decode($result);
-        }
         else
         {
-            // Default is XML
-            return new SimpleXMLElement($result);
+            // Default is JSON
+            return json_decode($result);
         }
     }
 
-    protected function callApi($apiFunction, $args=null,
-                               $responseType='json', $useHttpGet=FALSE)
+    protected function callApi($apiFunction, $args=null, $useHttpGet=FALSE)
     {
-        if (! in_array($responseType, $this->validResponseTypes))
-        {
-            throw new Exception($responseType .
-                                ' is not a valid response type.');
-        }
-
         $data = $this->getQueryString($args);
-        $url = $this->getUrlForCall($apiFunction, $responseType);
+        $url = $this->getUrlForCall($apiFunction);
         $result = $this->getHttpResult($url, $data, $useHttpGet);
-        return $this->parseResult($result, $apiFunction, $responseType);
+        return $this->parseResult($result, $apiFunction);
     }
 
     //
     // Helper function for the NLP calls.
     //
     protected function callNaturalLanguage($apiFunction, $cloud=null,
-                                           $text=null, $url=null,
-                                           $responseType='json')
+                                           $text=null, $url=null)
     {
         $args = array(
             'cloud' => $cloud,
             'text' => $text,
             'url' => $url,
         );
-        return $this->callApi($apiFunction, $args, $responseType);
+        return $this->callApi($apiFunction, $args);
     }
 
     //
     // Retreive the sentiment for a single URL or block of text.
     //
-    public function sentiment($text=null, $url=null, $responseType='json')
+    public function sentiment($text=null, $url=null)
     {
         if (is_array($text))
         {
                 $text = null;
             }
         }
-        return $this->callApi('score', array('text' => $text, 'url' => $url),
-                              $responseType);
+        return $this->callApi('score', array('text' => $text, 'url' => $url));
     }
 
     //
     // Bulk score multiple pieces of text (not urls!).
     //
-    public function bulkSentiment($items, $responseType='json')
+    public function bulkSentiment($items)
     {
         $itemsToScore = array();
         foreach (array_values($items) as $k => $v)
         {
             $itemsToScore['text' . $k] = $v;
         }
-        return $this->callApi('bulk-score', $itemsToScore, $responseType);
+        return $this->callApi('bulk-score', $itemsToScore);
     }
 
     //
     // Clean up a web page. It doesn't work well on home pages - it's
     // designed for content pages.
     //
-    public function clean_html($url=null, $responseType='json')
+    public function clean_html($url=null)
     {
         if (is_array($url)) extract($url, EXTR_IF_EXISTS);
         return $this->callApi('clean-html', array('url' => $url),
-                              $responseType, $useHttpGet=TRUE);
+                              $useHttpGet=TRUE);
     }
 
-    public function adjectives($cloud=null, $text=null, $url=null,
-                               $responseType='json')
+    public function adjectives($cloud=null, $text=null, $url=null)
     {
         if (is_array($cloud))
         {
                 $cloud = null;
             }
         }
-        return $this->callNaturalLanguage('adj', $cloud, $text, $url,
-                                          $responseType);
+        return $this->callNaturalLanguage('adj', $cloud, $text, $url);
     }
 
-    public function verbs($cloud=null, $text=null, $url=null,
-                          $responseType='json')
+    public function verbs($cloud=null, $text=null, $url=null)
     {
         if (is_array($cloud))
         {
                 $cloud = null;
             }
         }
-        return $this->callNaturalLanguage('verb', $cloud, $text, $url,
-                                          $responseType);
+        return $this->callNaturalLanguage('verb', $cloud, $text, $url);
     }
 
     public function ngrams($url=null, $text=null, $max=null, $min=null,
-                           $freq=null, $stopwords=null, $responseType='json')
+                           $freq=null, $stopwords=null)
     {
         if (is_array($url))
         {
             'stopwords' => $stopwords,
         );
         $useHttpGet = ($url != null);
-        return $this->callApi('ngrams', $args, $responseType, $useHttpGet);
+        return $this->callApi('ngrams', $args, $useHttpGet);
     }
 
     //
     // Convert english date indicators like "today", "tomorrow", "next week"
     // into date strings like 2011-01-12.
     //
-    public function dateExtraction($text=null, $relativeDate=null,
-                                   $responseType='json')
+    public function dateExtraction($text=null, $relativeDate=null)
     {
         if (is_array($text))
         {
             'text' => $text,
             'relative_date' => $relativeDate,
         );
-        return $this->callApi('extract-dates', $args, $responseType);
+        return $this->callApi('extract-dates', $args);
     }
 
     //

php/RepustateTest.php

         $this->checkCall('sentiment', $args);
     }
 
-    public function testSentimentForXml()
-    {
-        $args = array(
-            'text' => 'iPad',
-            'responseType' => 'xml',
-        );
-        $this->checkCall('sentiment', $args);
-    }
-
     public function testBulkSentiment()
     {
         $this->checkCall('bulkSentiment', array('I love candy'));