Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions example-json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
header('Content-Type: text/html; charset=utf-8');

if (!ini_get('date.timezone')) {
date_default_timezone_set('Europe/Prague');
}

require_once 'src/Feed.php';

$json = Feed::loadJsonfeed('https://www.jsonfeed.org/feed.json');

?>

<h1><?php echo htmlspecialchars($json->title) ?></h1>

<p><i><?php if(isset($json->description)){ echo htmlspecialchars($json->description); } ?></i></p>

<?php foreach ($json->items as $item): ?>
<h2><a href="<?php echo htmlspecialchars($item->url) ?>"><?php echo htmlspecialchars($item->title) ?></a>
<small><?php echo date('j.n.Y H:i', (int) $item->timestamp) ?></small></h2>

<?php if (isset($item->content_html)): ?>
<div><?php echo $item->content_html ?></div>
<?php else: ?>
<p><?php echo htmlspecialchars($item->summary) ?></p>
<?php endif ?>
<?php endforeach ?>
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Download Atom feed from URL:
$atom = Feed::loadAtom($url);
```

Download JSON feed from URL:

```php
$json = Feed::loadJsonfeed($url);
```

You can also enable caching:

```php
Expand Down
83 changes: 83 additions & 0 deletions src/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class Feed
/** @var string */
public static $userAgent = 'FeedFetcher-Google';

/** @var array */
public static $supportedJsonfeedVersions = [
'https://jsonfeed.org/version/1',
'https://jsonfeed.org/version/1.1'
];

/** @var SimpleXMLElement */
protected $xml;

Expand Down Expand Up @@ -69,6 +75,20 @@ public static function loadAtom($url, $user = null, $pass = null)
}


/**
* Loads JSON feed.
* @param string JSON feed URL
* @param string optional user name
* @param string optional password
* @return Feed
* @throws FeedException
*/
public static function loadJsonfeed($url, $user = null, $pass = null)
{
return self::fromJson(self::loadJson($url, $user, $pass));
}


private static function fromRss(SimpleXMLElement $xml)
{
if (!$xml->channel) {
Expand Down Expand Up @@ -114,6 +134,32 @@ private static function fromAtom(SimpleXMLElement $xml)
}


private static function fromJson(object $json)
{
if (!in_array($json->version, self::$supportedJsonfeedVersions, true)) {
throw new FeedException('Invalid feed.');
}

// generate 'url' & 'timestamp' tags
foreach ($json->items as $item) {
$item->timestamp = strtotime($item->date_published);

if (empty($item->content_text)) {
$item->summary = (string) strip_tags($item->content_html);
}
if (empty($item->content_html)) {
$item->summary = (string) strip_tags($item->content_text);
}
if (empty($item->summary)) {
$item->summary = (string) $item->content_text;
}
}
$feed = new self;
$feed->xml = $json;
return $feed;
}


/**
* Returns property value. Do not call directly.
* @param string tag name
Expand Down Expand Up @@ -197,6 +243,43 @@ private static function loadXml($url, $user, $pass)
}


/**
* Load JSON from cache or HTTP.
* @param string
* @param string
* @param string
* @return object
* @throws FeedException
*/
private static function loadJson($url, $user, $pass)
{
$e = self::$cacheExpire;
$cacheFile = self::$cacheDir . '/feed.' . md5(serialize(func_get_args())) . '.xml';

if (self::$cacheDir
&& (time() - @filemtime($cacheFile) <= (is_string($e) ? strtotime($e) - time() : $e))
&& $data = @file_get_contents($cacheFile)
) {
// ok
} elseif ($data = trim(self::httpRequest($url, $user, $pass))) {
if (self::$cacheDir) {
file_put_contents($cacheFile, $data);
}
} elseif (self::$cacheDir && $data = @file_get_contents($cacheFile)) {
// ok
} else {
throw new FeedException('Cannot load feed.');
}

$json = json_decode($data);
if(!$json){
throw new FeedException('Cannot parse feed.');
}

return $json;
}


/**
* Process HTTP request.
* @param string
Expand Down