Skip to content

Instantly share code, notes, and snippets.

@noameppel
Last active September 26, 2019 01:56
Show Gist options
  • Save noameppel/f7d50b15eb57988fb61094bb81076484 to your computer and use it in GitHub Desktop.
Save noameppel/f7d50b15eb57988fb61094bb81076484 to your computer and use it in GitHub Desktop.
Standalone PHP script to fetch latest Medium Posts
<?php
function getMediumData()
{
$handle = curl_init();
$url = "https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fmedium.com%2Ffeed%2Fmorpheus-network";
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
// Set the result output to be a string.
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($handle);
if (curl_error($handle)) {
$output = array();
}
curl_close($handle);
return json_decode($output);
}
$mediumData = getMediumData();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $mediumData->feed->title; ?></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
a:hover {
text-decoration: none;
}
.card-meta {
position: relative;
padding: 0.75rem 1.25rem;
line-height: normal;
}
.card-meta a {
color: #1C9963;
}
.card-meta-image {
height: 200px;
}
</style>
</head>
<body>
<div class="container">
<div class="row card-deck mt-3">
<?php
if (!empty($mediumData->items)) {
foreach ($mediumData->items as $medium) { ?>
<div class="col-md-4 mb-5">
<div class="card">
<a href="<?php echo $medium->link; ?>" class="stretched-link">
<img class="card-img-top card-meta-image" src="<?php echo $medium->thumbnail; ?>"></a>
<div class="card-body">
<h5 class="card-title"><?php echo $medium->title; ?></h5>
<?php
$start = strpos($medium->description, '<p>');
$end = strpos($medium->description, '</p>', $start);
$paragraph = substr($medium->description, $start, $end - $start + 4);
?>
<p class="card-text"><?php echo mb_strimwidth(html_entity_decode(strip_tags($paragraph)), 0, 140, "..."); ?></p>
</div>
<div class="d-inline-flex align-items-center card-meta">
<img src="https://cdn-images-1.medium.com/fit/c/36/36/1*WCCxWBKjf2YsLjj8lWQkLA.png"
class="mr-1">
<div>
<a href="https://medium.com/morpheus-network"
class="stretched-link">Morpheus.Network</a><br/>
<small class="text-muted"><?php echo date("M j", strtotime($medium->pubDate)); ?></small>
</div>
</div>
</div>
</div>
<?php }
}
?>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment