diff --git a/lib/httpclient.php b/lib/httpclient.php index 865fc9029e..bc513e71d0 100644 --- a/lib/httpclient.php +++ b/lib/httpclient.php @@ -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); diff --git a/plugins/Oembed/lib/oembedhelper.php b/plugins/Oembed/lib/oembedhelper.php index 3454ac7fc7..cd564b8339 100644 --- a/plugins/Oembed/lib/oembedhelper.php +++ b/plugins/Oembed/lib/oembedhelper.php @@ -139,19 +139,8 @@ class oEmbedHelper static function discover($url) { // @fixme ideally skip this for non-HTML stuff! - $body = self::http($url); - return self::discoverFromHTML($url, $body); - } + $body = HTTPClient::quickGet($url); - /** - * Partially ripped from OStatus' FeedDiscovery class. - * - * @param string $url source URL, used to resolve relative links - * @param string $body HTML body text - * @return mixed string with URL or false if no target found - */ - static function discoverFromHTML($url, $body) - { // DOMDocument::loadHTML may throw warnings on unrecognized elements, // and notices on unrecognized namespaces. $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE)); @@ -163,6 +152,18 @@ class oEmbedHelper throw new oEmbedHelper_BadHtmlException(); } + return self::discoverFromHTML($url, $dom); + } + + /** + * Partially ripped from OStatus' FeedDiscovery class. + * + * @param string $url source URL, used to resolve relative links + * @param string $body HTML body text + * @return mixed string with URL or false if no target found + */ + static function discoverFromHTML($url, DOMDocument $dom) + { // Ok... now on to the links! $feeds = array( 'application/json+oembed' => false, @@ -257,34 +258,9 @@ class oEmbedHelper */ static protected function json($url, $params=array()) { - $data = self::http($url, $params); + $data = HTTPClient::quickGet($url, null, $params); return json_decode($data); } - - /** - * Hit some web API and return data on success. - * @param string $url - * @param array $params - * @return string - */ - static protected function http($url, $params=array()) - { - $client = HTTPClient::start(); - if ($params) { - $query = http_build_query($params, null, '&'); - if (strpos($url, '?') === false) { - $url .= '?' . $query; - } else { - $url .= '&' . $query; - } - } - $response = $client->get($url); - if ($response->isOk()) { - return $response->getBody(); - } else { - throw new Exception('Bad HTTP response code: ' . $response->getStatus()); - } - } } class oEmbedHelper_Exception extends Exception diff --git a/plugins/Oembed/scripts/poll_oembed.php b/plugins/Oembed/scripts/poll_oembed.php index e49e1a50dd..a7e8c9bbc1 100755 --- a/plugins/Oembed/scripts/poll_oembed.php +++ b/plugins/Oembed/scripts/poll_oembed.php @@ -23,7 +23,7 @@ if (!have_option('u', 'url')) { $url = get_option_value('u', 'url'); -print "Contacting URL"; +print "Contacting URL\n"; $oEmbed = oEmbedHelper::getObject($url); var_dump($oEmbed);