forked from fahmiardi/slim-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedly.php
More file actions
64 lines (58 loc) · 2.01 KB
/
Copy pathEmbedly.php
File metadata and controls
64 lines (58 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Simplest possible helper for working with the Embed.ly API.
*/
class Embedly {
/**
* @type String The root URL for the Embed.ly API
*/
private static $root = 'http://api.embed.ly/1';
/**
* Query Embed.ly's oEmbed service for an embeddable version
* of the content available at the given URL.
* @param String $url The URL to query; all normalization of the URL
* is assumed to have been done externally.
* @param array $opts Options for submitting along with the URL;
* if omitted, the option "key" will be inserted and will carry
* the value stored in config option "embedly.key"
* @see config()
* @throws Exception If the API response code is anything other than 200
*/
function oembed($url, $opts = array()) {
$api = self::$root.'/oembed?'.http_build_query(array_merge(array(
'key' => config('embedly.key'),
'url' => $url
), $opts));
$req = Requests::get($api);
$res = json_decode($req->body);
if ($req->status_code !== 200) {
throw new Exception($res->error_message, $res->error_code);
} else {
return $res;
}
}
/**
* Query Embed.ly's oEmbed service for an embeddable version
* of the content available at the given URL.
* @param String $url The URL to query; all normalization of the URL
* is assumed to have been done externally.
* @param array $opts Options for submitting along with the URL;
* if omitted, the option "key" will be inserted and will carry
* the value stored in config option "embedly.key"
* @see config()
* @throws Exception If the API response code is anything other than 200
*/
function extract($url, $opts = array()) {
$api = self::$root.'/extract?'.http_build_query(array_merge(array(
'key' => config('embedly.key'),
'url' => $url
), $opts));
$req = Requests::get($api);
$res = json_decode($req->body);
if ($req->status_code !== 200) {
throw new Exception($res->error_message, $res->error_code);
} else {
return $res;
}
}
}