-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
41 lines (36 loc) · 1023 Bytes
/
api.php
File metadata and controls
41 lines (36 loc) · 1023 Bytes
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
<?php
class FeedsApi
{
public function __construct(){
}
protected function convertToJson($string){
$string = str_replace(array("\n", "\r", "\t"), '', $string);
$string = trim(str_replace('"', "'", $string));
$string = simplexml_load_string($string);
return json_encode($string);
}
public function fetch($params){
$feed = null;
//debug($params);
if(isset($params['source'])){
$handle = fopen($params['source'], "rb");
$feed = stream_get_contents($handle);
fclose($handle);
}
else echo 'A source is required.';
//debug($feed);
$feed = new SimpleXmlElement($feed);
if(isset($params['limit']) and $params['limit'] > 0){
// Not possible in SimpleXmlElement objects
}
//debug($feed); exit;
if(isset($params['raw']) and $params['raw'] === true){
echo $feed->asXml();
}
if(isset($params['convert']) and $params['convert'] == 'json'){
$feed = $this->convertToJson($feed->asXml());
}
//debug($feed);
echo $feed;
}
}