diff --git a/examples/oembed.php b/examples/oembed.php index 2f09ac9..d63aea3 100644 --- a/examples/oembed.php +++ b/examples/oembed.php @@ -62,25 +62,25 @@ '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", @@ -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; @@ -143,13 +144,20 @@ default: } - print ''; - print '
'; - if (array_key_exists('description', $oembed)) { - print $oembed['description']; + switch($oembed['type']) { + case 'error': + print '

Error

'; + print '
'; + break; + default: + print '
'; + print '
'; + if (array_key_exists('description', $oembed)) { + print $oembed['description']; + } + print '
via '.$oembed['provider_name'].'
'; + print '
'; + print '
'; } - print '
via '.$oembed['provider_name'].'
'; - print ''; - print '
'; } ?> diff --git a/src/Embedly/Embedly.php b/src/Embedly/Embedly.php index 7fc3ee8..c5fc11f 100644 --- a/src/Embedly/Embedly.php +++ b/src/Embedly/Embedly.php @@ -8,25 +8,25 @@ * @author Sven Eisenschmidt */ class Embedly { - + /** * * @const */ const VERSION = '0.1.0'; - + /** * * @var string */ protected $hostname = 'api.embed.ly'; - + /** * * @var string */ protected $key = null; - + /** * * @var array @@ -36,7 +36,7 @@ class Embedly { 'objectify' => 2, 'preview' => 1 ); - + /** * * @var string @@ -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. * @@ -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'; @@ -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(); } @@ -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' ); } @@ -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( @@ -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() { @@ -266,9 +268,9 @@ public function services() { } return $this->services; } - + /** - * + * * @return string */ public function services_regex() { @@ -276,18 +278,18 @@ public function services_regex() { $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 @@ -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)) { @@ -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; } }