Skip to content

Instantly share code, notes, and snippets.

@drogers98
Created May 19, 2014 19:01
Show Gist options
  • Save drogers98/ccde50cf873f94154540 to your computer and use it in GitHub Desktop.
Save drogers98/ccde50cf873f94154540 to your computer and use it in GitHub Desktop.
4good json feed
<?php
//include parsedown.php, to format the descriptions parsedown markup
include '../plugins/parsedown.php';
// header needed for https workaround
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$content = file_get_contents('https://4good.org/library-feed/Your_Feed_Here/latest.json',false,$context);
// function to remove the {by} in the description
function delete_all_between($beginning, $end, $description) {
$beginningPos = strpos($description, $beginning);
$endPos = strpos($description, $end);
if (!$beginningPos || !$endPos) {
return $description;
}
$textToDelete = substr($description, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
return str_replace($textToDelete, '', $description);
}
//Now decode and print
$json = json_decode($content, true);
foreach($json['results'] as $item) {
// define some vars
$Parsedown = new Parsedown();
$description = $item['description'];
// remove the weird {by}
$description = delete_all_between('{', '}', $description);
// If there is an image, lets show it
if ($item['image_thumb_url']){
echo "<a href='" . $item['url'] . "'><img class='FeedImage' src='https://4good.org" . $item['image_thumb_url'] . "'></a>";
}
//title, linked to content
echo "<h2><a href='" . $item['url'] . "'>" . $item['title'] . "</a></h2>";
//Author info and link
echo "<p class='meta'><a href='" . $item['owner_url'] . "'>" . $item['owner_name'] . "</a></p>";
//show the description
echo $Parsedown->text($description);
}
?>
@drogers98
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment