How to create CoWin vaccine tracker using PHP


Posted on June 4, 2021, 8 p.m. by Bishal     (  15114)


Card Thumbnail
Hi Everyone, Code With Bishal is back with a new project. So grab your PC and open your favourite Code Editor and start coding with me. In this blog, we will create a vaccine tracker using CoWin Public API. With the help of CoWin vaccine tracker we can track the date, time, address, availability, age group and much more .Please do read the official documentation (link below). All the downloadable resources are attached to this blog, You can easily download these with the help of the buttons below.

 

What is a vaccine tracker?

=> Our awesome, vaccine tracker gives visitors vaccination availability details based on Pincode and date !!

 

Where I can find the official documentation ?

Please read the documentation before continue reading our blog - https://apisetu.gov.in/public/marketplace/api/cowin

 

How can I use this vaccine tracker?

You can easily copy the code or can download the code from our website and can simply paste it on your server to get the vaccine tracker. After installing the code just open it on a browser, then fill the input field with the pincode and submit the form to get all the vaccination details for that Pincode and Date.
 

From where the data will be fetched ?

All the data will be fetched directly from Co-Win public API. Here is a link to the website: https://apisetu.gov.in/public/marketplace/api/cowin

 

How can I download the source code of the vaccine tracker?

Scroll the page to the bottom there you will find our download button to download the code of our vaccine tracker.

 

In this blog, you can learn, how to make a vaccine tracker using PHP - cURL By Code With Bishal. Just Copy The Code From Here And install it on your server. To Preview scroll the page to the beginning. If you are having any problem viewing the code just refresh the page. The vaccine tracker is a lightweight and easy-to-use PHP - cURL code that creates a vaccine tracker for you. In this blog, there are four sections on the webpage, in the first section, you can get a preview of the blog, in the second section there are few FAQs and an overview of the blog, in the third section there are few more coding blogs you might like, after that, you can copy the code and paste it [that's the fourth section] and from the last section, You can download the source code.

ScreenShots

vaccine tracker

 

vaccine tracker

 

Step by step guide of creating a vaccine tracker:

The first step is to add the basic syntax of PHP, you can add this from below.

 

  • Create a file with the name index.php

  • Open index.php

  • Add the opening and closing tags of PHP

 

<?php
?>

 

After adding the basic syntax. Make a php script to get the form value
 
<?php
if(isset($_POST['submit'])){
//code here
}
?>

 

Make variables for the pin code and date

 

<?php
if(isset($_POST['submit'])){
$Pincode = $_POST['pincode'];
$date = date("d-m-y");
}
?>

 

Now, let's make a HTML form to send the data to backend
 
<form action="" method="post">
<input type="number" name="pincode" id="pincode">
<input type="submit" value="Submit" name="submit">
</form>

 

Now, let's make the cURL request
 
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=110001&date=31-03-21",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
    ),
));

$response = curl_exec($curl);
$response = json_decode($response, True);

curl_close($curl);

 

vaccine tracker

Response from the API.

 

Replace the Pincode and the Date with the variables we created.
 
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=$Pincode&date=$date",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
    ),
));

$response = curl_exec($curl);
$response = json_decode($response, True);

curl_close($curl);

 

 

vaccine tracker

Response from the API.

 

To show these data in list create a for loop here .
 
<?php
foreach($response['centers'] as $list){
    echo "
    <ul>
    <li>".$list['center_id']."</li>
    <li>".$list['name']."</li>
    <li>".$list['address']."</li>
    <li>".$list['state_name']."</li>
    </ul>
    ";
}
?>

 

vaccine tracker

Response from the API.

 

And to get the values of nested arrays create another for loop inside the first loop .

 

<?php

foreach($response['centers'] as $list){
    foreach($list['sessions'] as $sessions){
    echo "
    <ul>
    <li>".$list['center_id']."</li>
    <li>".$list['name']."</li>
    <li>".$list['address']."</li>
    <li>".$list['state_name']."</li>
    <li>".$sessions['date']."</li>
    <li>".$sessions['available_capacity']."</li>
    <li>".$sessions['min_age_limit']."</li>
    <li>".$sessions['vaccine']."</li>
    </ul>
    ";
    }
}

?>

 

vaccine tracker

Response from the API.

 

 

We have successfully created our vaccine tracker, below is the code all together. This blog was all about backend tutoial and you can design it on your own way.

 

Source Code

1) PHP with HTML Code:

 

<?php

if(isset($_POST['submit'])){
    $Pincode = $_POST['pincode'];

    $date = date("d-m-y");

    $curl = curl_init();

    
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=$Pincode&date=$date",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_HTTPHEADER => array(
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
        ),
    ));
    
    $response = curl_exec($curl);
    $response = json_decode($response, True);
    
    curl_close($curl);
}
    ?>
<form action="" method="post">
<input type="number" name="pincode" id="pincode">
<input type="submit" value="Submit" name="submit">
</form>


<?php

foreach($response['centers'] as $list){
    foreach($list['sessions'] as $sessions){
    echo "
    <ul>
    <li>".$list['center_id']."</li>
    <li>".$list['name']."</li>
    <li>".$list['address']."</li>
    <li>".$list['state_name']."</li>
    <li>".$sessions['date']."</li>
    <li>".$sessions['available_capacity']."</li>
    <li>".$sessions['min_age_limit']."</li>
    <li>".$sessions['vaccine']."</li>
    </ul>
    ";
    }
}

?>




Leave a Comment:
No comments Found. Be the First one to comment