Random Quotes Generator using PHP API

The following source code demonstrates a simple PHP API to get random quote. We simply need to make a GET request to the API endpoint : https://api.quotable.io/random and we will get a random quote in response.

Full Source Code :

<?php

$api_url = 'https://api.quotable.io/random';
$quote = json_decode(file_get_contents($api_url));
?>

<style>
  body{
    background: #f4f4f4;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .box{
    background: #fff;
    padding:20px;
    width: fit-content;
    border: 1px solid rgba(0,0,0,0.1);
    font-size: 19px;
      line-height: 22px;
      color: #555;
  }
  .tag{
    background: #ddd;
    padding: 10px;
    border-radius: 10px;
    margin-right: 15px;
  }
  .content{
    line-height: 24px;
  }
</style>

<div class="box">
  <h3 class="content"><?php echo $quote->content;?></h3>
  <h5>- By <?php echo $quote->author;?></h5>
  <div class="tag-wrapper">
    <?php foreach($quote->tags as $tag){?>
      <span class="tag">
        # <?php echo $tag;?>
      </span>
    <?php } ?>
  </div>
</div>

Follow this video for complete guidance :

Recommended For You

About the Author: Ritesh Ghimire

Ritesh is a Computer Engineer, Web Developer and an active cricket follower.