How to create CoWin vaccine tracker using PHP - cURL


Posted on June 4, 2021, at 8:00 PM [IST], created by Admin, Website: codewithbishal.com


vaccine tracker

CoWin vaccine tracker using PHP - cURL :

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/api/cowin/cowin-public-v2

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/api/cowin/cowin-public-v2

Vaccine Tracker

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.

Overview of the Blog :

=> 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.


You might like this :

How to create responsive column cards Filter images using JavaScript
How to create accordion using JavaScript How to create movie ticket using CSS Glowing Button using CSS




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.

Step 1

=> Add the basic syntax of PHP
              
<?php ?>
After adding the basic syntax. Make a php script to get the form value
<?php

if(isset($_POST['submit'])){

}
?>
Make variables for the pincode 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 here is the code all together. This blog was all about backend tutoial and you can design it on your own way

Copy or Download the code from below


<?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:

Comments:



Search