Skip to content

Commit

Permalink
Updates for batter api compat.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Corsaro committed Jan 13, 2012
1 parent 99f372f commit 5959553
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 71 deletions.
40 changes: 24 additions & 16 deletions examples/oembed.php
Expand Up @@ -62,25 +62,25 @@
</head>
<body>
<?php
if (!getenv('EMBEDLY_KEY')) {
throw new \Exception('environmental variable EMBEDLY_KEY must be defined');
}
require_once "../src/Embedly/Embedly.php";
$api = new Embedly\Embedly(array(
'user_agent' => 'Mozilla/5.0 (compatible; embedly/example-app; support@embed.ly)'
'user_agent' => 'Mozilla/5.0 (compatible; embedly/example-app; support@embed.ly)',
'key' => getenv('EMBEDLY_KEY')
));

$urls = array(
"http://www.slideshare.net/doina/happy-easter-from-holland-slideshare", "http://www.scribd.com/doc/13994900/Easter",
"http://screenr.com/t9d",
"http://www.5min.com/Video/How-to-Decorate-Easter-Eggs-with-Decoupage-142076462",
"http://www.youtube.com/watch?v=iF3gzfel-a0",
"http://www.howcast.com/videos/328008-How-To-Marble-Easter-Eggs",
"http://img402.yfrog.com/i/mfe.jpg/",
"http://tweetphoto.com/14784359",
"http://www.flickr.com/photos/10349896@N08/4490293418/",
"http://imgur.com/6pLoN",
"http://twitgoo.com/1as",
"http://www.qwantz.com/index.php?comic=1686",
"http://www.23hq.com/mhg/photo/5498347",
"http://www.youtube.com/watch?v=gghKdx558Qg",
"http://www.justin.tv/easter7presents",
"http://revision3.com/hak5/DualCore",
"http://www.dailymotion.com/video/xcstzd_greek-wallets-tighten-during-easter_news",
"http://www.collegehumor.com/video:1682246",
Expand All @@ -90,18 +90,19 @@
"http://www.metacafe.com/watch/105023/the_easter_bunny/",
"http://blip.tv/file/449469",
"http://video.google.com/videoplay?docid=-5427138374898988918&q=easter+bunny&pl=true",
"http://video.yahoo.com/watch/7268801/18963438",
"http://www.viddler.com/explore/BigAppleChannel/videos/113/",
"http://www.liveleak.com/view?i=e0b_1239827917",
"http://www.hulu.com/watch/67313/howcast-how-to-make-braided-easter-bread",
"http://www.fancast.com/tv/It-s-the-Easter-Beagle,-Charlie-Brown/74789/1078053475/Peanuts:-Specials:-It-s-the-Easter-Beagle,-Charlie-Brown/videos",
"http://www.funnyordie.com/videos/f6883f54ae/the-unsettling-ritualistic-origin-of-the-easter-bunny",
"http://vimeo.com/10446922",
"http://www.ted.com/talks/jared_diamond_on_why_societies_collapse.html",
"http://www.thedailyshow.com/watch/thu-december-14-2000/intro---easter",
);

$oembeds = $api->oembed(array('urls' => $urls)); //, 'maxwidth' => 200));
$oembeds = array();
for ($i = 0; $i*20 < count($urls); $i++) {
$oembeds = array_merge($oembeds, $api->oembed(array('urls' => array_slice($urls, $i*20, 20))));
}

foreach ($oembeds as $k => $oembed) {
$oembed = (array) $oembed;
Expand Down Expand Up @@ -143,13 +144,20 @@
default:
}

print '</div>';
print '<div class="description">';
if (array_key_exists('description', $oembed)) {
print $oembed['description'];
switch($oembed['type']) {
case 'error':
print '<p>Error</p>';
print '<div class="clear"></div>';
break;
default:
print '</div>';
print '<div class="description">';
if (array_key_exists('description', $oembed)) {
print $oembed['description'];
}
print '<div class="via">via <a href="'.$oembed['provider_url'].'">'.$oembed['provider_name'].'</a></div>';
print '</div>';
print '<div class="clear"></div>';
}
print '<div class="via">via <a href="'.$oembed['provider_url'].'">'.$oembed['provider_name'].'</a></div>';
print '</div>';
print '<div class="clear"></div>';
}
?></body></html>
112 changes: 57 additions & 55 deletions src/Embedly/Embedly.php
Expand Up @@ -8,25 +8,25 @@
* @author Sven Eisenschmidt <sven.eisenschmidt@gmail.com>
*/
class Embedly {

/**
*
* @const
*/
const VERSION = '0.1.0';

/**
*
* @var string
*/
protected $hostname = 'api.embed.ly';

/**
*
* @var string
*/
protected $key = null;

/**
*
* @var array
Expand All @@ -36,7 +36,7 @@ class Embedly {
'objectify' => 2,
'preview' => 1
);

/**
*
* @var string
Expand Down Expand Up @@ -70,15 +70,13 @@ public function __construct(array $args = array())
}
if ($args['hostname']) {
$this->hostname = $args['hostname'];
} else if ($args['key']) {
$this->hostname = 'pro.embed.ly';
}
if ($args['api_version']) {
$this->api_version = array_merge($this->api_version, $args['api_version']);
}
}
/**

/**
*
* Flexibly parse host strings.
*
Expand All @@ -92,7 +90,7 @@ public function __construct(array $args = array())
* @param string $host
* @return array
*/
protected function parse_host($host)
protected function parse_host($host)
{
$port = 80;
$protocol = 'http';
Expand Down Expand Up @@ -129,57 +127,57 @@ protected function parse_host($host)
'port' => $port
);
}

/**
*
*
* @return string|array
*/
public function oembed($params)
{
{
return $this->apicall($this->api_version['oembed'], 'oembed', $params);
}

/**
*
* @param string|array $params
*
* @param string|array $params
* @return object
*/
public function preview($params)
{
return $this->apicall($this->api_version['preview'], 'preview', $params);
}

/**
*
* @param array $params
*
* @param array $params
* @return object
*/
public function objectify($params)
{
return $this->apicall($this->api_version['objectify'], 'objectify', $params);
}

/**
*
*
* @return string
*/
public function api_version()
{
return $this->api_version;
}

/**
*
* @param string $version
* @param array $action
* @param array $params
*
* @param string $version
* @param array $action
* @param array $params
* @return object
*/
public function apicall($version, $action, $params)
{
$justone = is_string($params);
$params = self::paramify($params);

if (!array_key_exists('urls', $params)) {
$params['urls'] = array();
}
Expand All @@ -206,7 +204,7 @@ public function apicall($version, $action, $params)
unset($params['urls'][$i]);
$rejects[$i] = (object)array(
'error_code' => '401',
'error_message' => 'This service requires an Embedly Pro account',
'error_message' => 'This service requires an Embedly key',
'type' => 'error'
);
}
Expand All @@ -216,10 +214,14 @@ public function apicall($version, $action, $params)
$result = array();

if (sizeof($rejects) < sizeof($params['urls'])) {
if (count($params['urls']) > 20) {
throw new \Exception(
sprintf("Max of 20 urls can be queried at once, %s passed",
count($params['urls'])));
}
$path = sprintf("%s/%s", $version, $action);
$url_parts = $this->parse_host($this->hostname);
$apiUrl = sprintf("%s%s?%s", $url_parts['url'], $path, $this->q($params));
//print("\ncalling $apiUrl\n");

$ch = curl_init($apiUrl);
$this->setCurlOptions($ch, array(
Expand All @@ -240,16 +242,16 @@ public function apicall($version, $action, $params)
foreach ($rejects as $obj) {
array_push($merged_result, $obj);
}

if($justone) {
return array_shift($merged_result);
}

return $merged_result;
}

/**
*
*
* @return array
*/
public function services() {
Expand All @@ -266,28 +268,28 @@ public function services() {
}
return $this->services;
}

/**
*
*
* @return string
*/
public function services_regex() {
$services = $this->services();
$regexes = array_map(array(__CLASS__, 'reg_imploder'), $services);
return '#'.implode('|', $regexes).'#i';
}

/**
*
*
* @return string
*/
protected function q($params) {
$pairs = array_map(array(__CLASS__, 'url_encode'), array_keys($params), array_values($params));
return implode('&', $pairs);
}

/**
*
*
* @param resource $ch
* @param array $headers
* @return void
Expand All @@ -301,51 +303,51 @@ protected function setCurlOptions(&$ch, $headers = array())
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
}

/**
*
*
* @param resource $ch
* @return string
*/
protected function curlExec(&$ch)
{
{
$res = curl_exec($ch);
if (false === $res) {
throw new \Exception(curl_error($ch), curl_errno($ch));
}
return $res;
}


/**
*
*
* @param string $r
* @return string
*/
public static function reg_delim_stripper($r)
public static function reg_delim_stripper($r)
{
# we need to strip off regex delimeters and options to make
# one giant regex
return substr($r, 1, -2);
}
}

/**
*
*
* @param stdClass $o
* @return string
*/
public static function reg_imploder(\stdClass $o)
public static function reg_imploder(\stdClass $o)
{
return implode('|', array_map(array(__CLASS__, 'reg_delim_stripper'), $o->regex));
}

/**
*
*
* @param string $key
* @param string|array $value
* @return string
*/
public static function url_encode($key, $value)
public static function url_encode($key, $value)
{
$key = urlencode($key);
if (is_array($value)) {
Expand All @@ -357,16 +359,16 @@ public static function url_encode($key, $value)
}

/**
*
*
* @param string $input
* @return array
*/
public static function paramify($input)
public static function paramify($input)
{
if(is_string($input)) {
return array('urls' => $input);
}

return $input;
}
}

1 comment on commit 5959553

@perek
Copy link

@perek perek commented on 5959553 Jan 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delicious batter.

Please sign in to comment.