Update: Check our latest article for getting email notification for CoWIN Vaccine slot availability.

As the country open up Covid 19 vaccination for 18+ age group on COWIN portal, there is a acute shortage of vaccines and appointment slots. In many states, only private hospitals have started vaccination for 18+ age group but with very limited number of slots available.

Using a PHP script, we can monitor the vaccine availability and get email notification.

Step 1: Deploy a Linux server in India

Even though you can run the script on local computer, it is best to deploy it on a server as you can run it 24×7. I recommend using AWS to deploy a Ubuntu server with PHP and Apache or Nginx. Make sure the server is located in India as the API is blocked outside India.

Step 2: Prerequisites 

  1. Download PHPMailer and copy PHPMailer.php and SMTP.php files to web directory. (https://github.com/PHPMailer/PHPMailer/tree/master/src)
  2. Create a Gmail account to send notification emails. Enable less secure apps or use app specific password if you have 2 step verification on.
  3. Identify the district code for your district from https://getjab.in/.

Step 3: Modify the PHP script as required

<html>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require("PHPMailer.php");
require("SMTP.php");
date_default_timezone_set('Asia/Kolkata'); // Your time zone
$distid=781; // Your district code
$today = date('d-m-Y');
for($j=1; $j<=10; $j++)
{
echo '<h2>Mod time '.$today.'</h2>';
$links='https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id='.$distid.'&date='.$today.'';
$agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $links);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1) ;
curl_exec($curl);
$file_get_contents=curl_exec($curl);
if (!is_null($file_get_contents)){
$parse=json_decode($file_get_contents);
$data=$parse->sessions;
//echo $file_get_contents;
$arrLength = count($data);
for($i=0; $i<$arrLength; $i++)
{
if($data[$i]->min_age_limit==18)
{
echo '<p>';
$name=$data[$i]->name;
echo '<b>Name:</b> '.$data[$i]->name;
echo '<br>';
$address=$data[$i]->address;
echo '<b>Address:</b> '.$data[$i]->address;
echo '<br>';
$pincode=$data[$i]->pincode;
echo '<b>District:</b> '.$data[$i]->pincode;
echo '<br>';
$district=$data[$i]->district_name;
echo '<b>Pincode:</b> '.$data[$i]->pincode;
echo '<br>';
$date=$data[$i]->date;
echo '<b>Date:</b> '.$data[$i]->date;
echo '<br>';
$fee=$data[$i]->fee;
echo '<b>Fee:</b> '.$data[$i]->fee;
echo '<br>';
$age=$data[$i]->min_age_limit;
echo '<b>Age:</b> '.$data[$i]->min_age_limit;
echo '<br>';
$capacity=$data[$i]->available_capacity;
echo '<b>Capacity:</b> '.$data[$i]->available_capacity;
echo '<br>';
$vaccine=$data[$i]->vaccine;
echo '<b>Vaccine:</b> '.$data[$i]->vaccine;

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'username@gmail.com';
$mail->Password = 'password'; //password for your Gmail account
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('username@gmail.com', 'Vaccine Alert');
$mail->addAddress('username@gmail.com', 'Vaccine');
$mail->Subject = 'Vaccine Center Found';
$mail->isHTML(true);
$mailContent = "<h1>Vaccine center details</h1>
<p>
<b>Name:</b> $name
<br>
<b>Address:</b> $address
<br>
<b>District:</b> $district
<br>
<b>Pincode:</b> $pincode
<br>
<b>Date:</b> $date
<br>
<b>Fee:</b> $fee
<br>
<b>Age:</b> $age
<br>
<b>Capacity:</b> $capacity
<br>
<b>Vaccine:</b> $vaccine
<br>
<b>Book Now: </b><a href=https://selfregistration.cowin.gov.in/>https://selfregistration.cowin.gov.in/</a>";
$mail->Body = $mailContent;
if($mail->send()){
echo '<p>';
echo 'Email has been sent';
}else{
echo '<p>';
echo 'Email could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
}
}
$repeat = strtotime("+1 day",strtotime($today));
$today = date('d-m-Y',$repeat);
}
?>
</html>

Step 4: Upload files and configure cronjob

  1. Upload the files to your web directory (/var/www/html/).
  2. Create a cron job and add below (sudo crontab -e)
  3. */5 * * * * IS_CRON=1 /usr/bin/php /var/www/html/vaccine.php

The cron job will run the PHP script every 5mins and sends you notification if it finds a vaccine slot available in your city. It also scans for slot in next 10 days time and not just current date.

There is a rate limit for the API, so please avoid running it every min. You can deploy multiple scripts for each district you want to monitor.

2 thoughts on “COWIN vaccine appointment slot tracker/notification for age 18+ using PHP script”
    1. Updated the article with new code. There is now requirement of user agent to be sent along with API request.

Leave a Reply

Your email address will not be published. Required fields are marked *