]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/httpclient.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / lib / httpclient.php
index 865fc9029e847134b0668fc3ba9eee7b8eab33aa..4990d5ba844e192e3e11295270341e78a528f44d 100644 (file)
@@ -177,8 +177,17 @@ class HTTPClient extends HTTP_Request2
     /**
      * Quick static function to GET a URL
      */
-    public static function quickGet($url, $accept=null)
+    public static function quickGet($url, $accept=null, $params=array())
     {
+        if (!empty($params)) {
+            $params = http_build_query($params, null, '&');
+            if (strpos($url, '?') === false) {
+                $url .= '?' . $params;
+            } else {
+                $url .= '&' . $params;
+            }
+        }
+
         $client = new HTTPClient();
         if (!is_null($accept)) {
             $client->setHeader('Accept', $accept);
@@ -191,6 +200,16 @@ class HTTPClient extends HTTP_Request2
         return $response->getBody();
     }
 
+    public static function quickGetJson($url, $params=array())
+    {
+        $data = json_decode(self::quickGet($url, null, $params));
+        if (is_null($data)) {
+            common_debug('Could not decode JSON data from URL: '.$url);
+            throw new ServerException('Could not decode JSON data from URL');
+        }
+        return $data;
+    }
+
     /**
      * Convenience function to run a GET request.
      *
@@ -220,13 +239,16 @@ class HTTPClient extends HTTP_Request2
             $this->config['follow_redirects'] = $follow_redirects;
             return $this->doRequest($url, self::METHOD_HEAD, $headers);
         } catch (Exception $e) {
-            // Let the exception go on its merry way.
-            throw $e;
-        } finally {
             // reset to the old value
             $this->config['follow_redirects'] = $old_follow;
+
+            // Let the exception go on its merry way.
+            throw $e;
         }
         //we've either returned or thrown exception here
+
+        // reset to the old value
+        $this->config['follow_redirects'] = $old_follow;
     }
 
     /**