oembed.php
Gesponsorde koppelingen
PHP script bestanden
Code (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
function oembed_get_data($video_url, array $params = array(), &$error = null)
{
$html = @file_get_contents($video_url);
if (!$html)
{
$error = 'Could not download video page';
return false;
}
$links = oebmed_parse_links($html);
// discover oembed api location using by searching for <link/> elements
foreach ($links as $link)
{
if (!isset($link['type']) || empty($link['href']))
continue;
$oembed_url = $link['href'];
if (!empty($params))
$oembed_url .= (strpos($oembed_url, '?') === false ? '?' : '&')
. http_build_query($params);
if ($link['type'] == 'application/xml+oembed' || $link['type'] == 'text/xml+oembed')
return oembed_get_xml_data($link['href'], $error);
if ($link['type'] == 'application/json+oembed')
return oembed_get_json_data($link['href'], $error);
}
// fallback using embed.ly
$params = array_merge($params, array('url' => $video_url, 'format' => 'json'));
$embedly_uri = 'http://api.embed.ly/1/oembed?' . http_build_query($params);
return oembed_get_json_data($embedly_uri, $error);
}
function oebmed_parse_links($html)
{
$links = array();
// pattern gesplitst voor PHPhulp highlighting
if (preg_match_all('{<link ([^<>]+)/?'.'>}i', $html, $matches))
foreach ($matches[1] as $attributes_str)
{
preg_match_all('{([a-z]+)=(["\'])(.+?)\2}i', $attributes_str, $amatches, PREG_SET_ORDER);
$attributes = array();
foreach ($amatches as $amatch)
$attributes[$amatch[1]] = html_entity_decode($amatch[3]);
$links[] = $attributes;
}
return $links;
}
function oembed_get_xml_data($oembed_url, &$error = null)
{
$xml = @simplexml_load_file($oembed_url);
if (!$xml)
{
$error = 'Could not parse or load oembed xml data';
return false;
}
$data = new stdClass;
foreach ($xml as $property => $value)
$data->$property = (string) $value;
return $data;
}
function oembed_get_json_data($oembed_url, &$error = null)
{
$json = @file_get_contents($oembed_url);
if (!$json)
{
$error = 'Could not download oembed json data';
return false;
}
$data = @json_decode($json);
if (!$data)
{
$error = 'Could not parse oembed json data';
return false;
}
return (object) $data;
}
function oembed_get_data($video_url, array $params = array(), &$error = null)
{
$html = @file_get_contents($video_url);
if (!$html)
{
$error = 'Could not download video page';
return false;
}
$links = oebmed_parse_links($html);
// discover oembed api location using by searching for <link/> elements
foreach ($links as $link)
{
if (!isset($link['type']) || empty($link['href']))
continue;
$oembed_url = $link['href'];
if (!empty($params))
$oembed_url .= (strpos($oembed_url, '?') === false ? '?' : '&')
. http_build_query($params);
if ($link['type'] == 'application/xml+oembed' || $link['type'] == 'text/xml+oembed')
return oembed_get_xml_data($link['href'], $error);
if ($link['type'] == 'application/json+oembed')
return oembed_get_json_data($link['href'], $error);
}
// fallback using embed.ly
$params = array_merge($params, array('url' => $video_url, 'format' => 'json'));
$embedly_uri = 'http://api.embed.ly/1/oembed?' . http_build_query($params);
return oembed_get_json_data($embedly_uri, $error);
}
function oebmed_parse_links($html)
{
$links = array();
// pattern gesplitst voor PHPhulp highlighting
if (preg_match_all('{<link ([^<>]+)/?'.'>}i', $html, $matches))
foreach ($matches[1] as $attributes_str)
{
preg_match_all('{([a-z]+)=(["\'])(.+?)\2}i', $attributes_str, $amatches, PREG_SET_ORDER);
$attributes = array();
foreach ($amatches as $amatch)
$attributes[$amatch[1]] = html_entity_decode($amatch[3]);
$links[] = $attributes;
}
return $links;
}
function oembed_get_xml_data($oembed_url, &$error = null)
{
$xml = @simplexml_load_file($oembed_url);
if (!$xml)
{
$error = 'Could not parse or load oembed xml data';
return false;
}
$data = new stdClass;
foreach ($xml as $property => $value)
$data->$property = (string) $value;
return $data;
}
function oembed_get_json_data($oembed_url, &$error = null)
{
$json = @file_get_contents($oembed_url);
if (!$json)
{
$error = 'Could not download oembed json data';
return false;
}
$data = @json_decode($json);
if (!$data)
{
$error = 'Could not parse oembed json data';
return false;
}
return (object) $data;
}