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 :


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

    background: #f4f4f4;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    width: fit-content;
    border: 1px solid rgba(0,0,0,0.1);
    font-size: 19px;
      line-height: 22px;
      color: #555;
    background: #ddd;
    padding: 10px;
    border-radius: 10px;
    margin-right: 15px;
    line-height: 24px;

<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;?>
    <?php } ?>

Follow this video for complete guidance :

ALSO READ  Calculate Age from Date of Birth (DOB) using PHP

Comments are closed.