-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoembed.php
78 lines (77 loc) · 2.06 KB
/
oembed.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* http://www.oembed.com/
* @author olamedia
*/
class oembed{
protected $_apiEndpoint = null;
protected $_apiFormat = null;
protected $_url = null;
protected $_plainResponse = null;
public function __construct($url = null){
$this->_url = $url;
}
public function setUrl($url){
$this->_url = $url;
}
public function setApi($url){
$this->_apiEndpoint = $url;
}
public function setFormat($format){
$this->_apiFormat = $format;
}
public function discoverApi(){
// <link rel="alternate" type="application/json+oembed" href="
// <link rel="alternate" type="text/xml+oembed" href="
}
protected function _isXml(){
if (!$this->_getPlainResponse()) return false;
return (substr($this->_plainResponse,0,5) == '<?xml');
}
protected function _getArray(){
if (!$this->_getPlainResponse()) return false;
//echo htmlspecialchars($this->_plainResponse);
if ($this->_isXml()){
return (array) simplexml_load_string($this->_plainResponse);
}
return json_decode($this->_plainResponse, true);
}
protected function _getRequestUrl(){
$url = $this->_apiEndpoint.'?url='.urlencode($this->_url);
if ($this->_apiFormat == 'json') {
$url .= '&format=json';
}else{
$url .= '&format=xml';
}
//echo $url;
return $url;
}
protected function _getPlainResponse(){
if ($this->_plainResponse !== null) return true;
if ($this->_plainResponse === false) return false;
if ($this->_url === null) return false;
$this->_plainResponse = file_get_contents($this->_getRequestUrl());
if ($this->_plainResponse) return true;
}
public function getType(){ // photo/video/link/rich
if ($a = $this->_getArray()){
return $a['type'];
}
return false;
}
public function getEmbedSrc(){ // photo/video/link/rich
if ($a = $this->_getArray()){
$html = $a['html'];
if (preg_match("#<embed[^>]* src='([^']+)'#ims", $html, $subs)){
$src = $subs[1];
return $src;
}elseif (preg_match("#<embed[^>]* src=\"([^\"]+)\"#ims", $html, $subs)){
$src = $subs[1];
return $src;
}else{
echo htmlspecialchars($html);
}
}
return false;
}
}