HTTPClient get $params array and oEmbedHelper uses it
This commit is contained in:
parent
fbe0e68617
commit
b7edac2610
@ -177,8 +177,17 @@ class HTTPClient extends HTTP_Request2
|
|||||||
/**
|
/**
|
||||||
* Quick static function to GET a URL
|
* 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();
|
$client = new HTTPClient();
|
||||||
if (!is_null($accept)) {
|
if (!is_null($accept)) {
|
||||||
$client->setHeader('Accept', $accept);
|
$client->setHeader('Accept', $accept);
|
||||||
|
@ -139,19 +139,8 @@ class oEmbedHelper
|
|||||||
static function discover($url)
|
static function discover($url)
|
||||||
{
|
{
|
||||||
// @fixme ideally skip this for non-HTML stuff!
|
// @fixme ideally skip this for non-HTML stuff!
|
||||||
$body = self::http($url);
|
$body = HTTPClient::quickGet($url);
|
||||||
return self::discoverFromHTML($url, $body);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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,
|
// DOMDocument::loadHTML may throw warnings on unrecognized elements,
|
||||||
// and notices on unrecognized namespaces.
|
// and notices on unrecognized namespaces.
|
||||||
$old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
|
$old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
|
||||||
@ -163,6 +152,18 @@ class oEmbedHelper
|
|||||||
throw new oEmbedHelper_BadHtmlException();
|
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!
|
// Ok... now on to the links!
|
||||||
$feeds = array(
|
$feeds = array(
|
||||||
'application/json+oembed' => false,
|
'application/json+oembed' => false,
|
||||||
@ -257,34 +258,9 @@ class oEmbedHelper
|
|||||||
*/
|
*/
|
||||||
static protected function json($url, $params=array())
|
static protected function json($url, $params=array())
|
||||||
{
|
{
|
||||||
$data = self::http($url, $params);
|
$data = HTTPClient::quickGet($url, null, $params);
|
||||||
return json_decode($data);
|
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
|
class oEmbedHelper_Exception extends Exception
|
||||||
|
@ -23,7 +23,7 @@ if (!have_option('u', 'url')) {
|
|||||||
|
|
||||||
$url = get_option_value('u', 'url');
|
$url = get_option_value('u', 'url');
|
||||||
|
|
||||||
print "Contacting URL";
|
print "Contacting URL\n";
|
||||||
|
|
||||||
$oEmbed = oEmbedHelper::getObject($url);
|
$oEmbed = oEmbedHelper::getObject($url);
|
||||||
var_dump($oEmbed);
|
var_dump($oEmbed);
|
||||||
|
Loading…
Reference in New Issue
Block a user