/
Tools.php
55 lines (48 loc) · 1.55 KB
/
Tools.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
<?php
class Tools {
public static function arrayToXML($a)
{
if (! is_array($a)) {
return false;
}
$items = new SimpleXMLElement("<items></items>");
foreach($a as $b) {
$c = $items->addChild('item');
$c_keys = array_keys($b);
foreach($c_keys as $key) {
if ($key == 'uid') {
$c->addAttribute('uid', $b[$key]);
}
elseif ($key == 'arg') {
$c->addAttribute('arg', $b[$key]);
}
else {
$c->addChild($key, $b[$key]);
}
}
}
return $items->asXML();
}
public static function fetch($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Easy Fetch by Jeff Johns @phpfunk');
$page = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return ($info['http_code'] == '200') ? $page : null;
}
public static function getTrackArtwork($type, $id)
{
$html = self::fetch('http://open.spotify.com/' . $type . '/' . $id);
if (! empty($html)) {
preg_match_all('/.*?og:image.*?content="(.*?)">.*?/is', $html, $m);
return (isset($m[1][0])) ? $m[1][0] : '';
}
return '';
}
}