HTTPClient get $params array and oEmbedHelper uses it

This commit is contained in:
Mikael Nordfeldth 2015-11-30 01:28:18 +01:00
parent fbe0e68617
commit b7edac2610
3 changed files with 25 additions and 40 deletions

View 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);

View File

@ -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

View File

@ -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);