Automate your WhatsApp messages with PHP and directly send them from your own whatsapp number. Not stuck with cost per message and only a monthly fee.

automate whatsapp messages php

Automate WhatsApp messages with PHP

Since a pretty short time you keep seeing whatsapp messages being automated more and more. Because we are in this business and develop certain applications who could use this functionality, I decided to dive into this. Automating WhatsApp messages with PHP is done by a API call where you could decide to use different third party’s in order for your message handling.

WhatsApp API’s Available

WhatsApp API with Phonenumber provider

There are a couple of WhatsApp API providers to be found on Google, where the most popular one currently is known as ‘Twillio‘. When you are using Twillio for your services keep in mind that you have to ‘buy’ a phone number and do have to pay for each message you will be sending through their services. The starting price is around 0,08$ a whatsapp message. This price will be determined by the amount of messages that you are actually sending through their services.

Web- WhatsApp PHP API

Next to Twillio I have found a decent amount of other platforms for WhatsApp API handling but there was one platform which stood out the most. Because you do have to pay a standaard amount of dollars for each message this was something like a needle in my eye. Another web-service, another monthly payment, you get the it..

But lucky as we ware, we stumbled upon Chat-Api.com . Chat-API offers the possibility for a monthly subscription with 6000 whatsapp messages per day for 39,00$ a month. If you would send 15 whatsapp messages a day in this case, than Chat-API.com will be a lot cheaper than twillio.

Another advantage is that you can actually use a Phone number from your own country or your own WhatsApp phone number if you would like to. The Chat-API is connected through a “Web-WhatsApp” browser and keeps the connection active in order to send messages with the connected phone. Keep in mind that the phone should not be turned off in order to make it actually send the WhatsApp messages. 

PHP Chat-API.com WhatsApp script

<?php
$telefoon = ‘0031639011166’; // Assign the phone number to string which includes the 0031 country code.
$datum =  date(“l jS of F Y h:i:s A”); // Creating a date with date(); function to add to the message.
$tekst = ‘Dit bericht is verstuurd om:’. $datum; // Assign $tekst variable with this messages has been send: $date (Php date)

$data = [ // creating an array named $data
‘phone’ => $telefoon, // Adding the $telefoon variable in the array and assigning it to the phone label.
‘body’ => $tekst, // Here we add the text for the message we will send with the json array.
]; // Close the array

$json = json_encode($data); // Encoding the array so it can be send as JSON to Chat-API.com
$token = ‘here_your_token_id’; // Chat with your token from chat-api.com between the quotes.
$instanceId = ‘here_your_instance_id’; // And insert instance id here between the quotes.

// Now we are going to assign an url with our instanceId and Token id to make the call to.
// In PHP you escape single quote variables by putting ‘.$variable.’ between the tekst.
$url = ‘https://api.chat-api.com/instance’.$instanceId.’/message?token=’.$token;

// To post to JSON we have to apply some settings to our stream_context_create in order to send it with the file_get_contents function. In this case is a POST because it’s required like this by Chat-Api.com
$options = stream_context_create([‘http’ => [
‘method’=> ‘POST’, // In this case no GET or REQUEST but a POST method.
‘header’=> ‘Content-type: application/json’, // We are telling the browser the POST is of content type: JSON.
‘content’ => $json] // Add the JSON Array we created before
]); // Close the array

// Create the call to the $URL with the $OPTIONS we created in order to send the message.
$result = file_get_contents($url, false, $options);

?>

The Chat-API can be easily used by scanned a QR code on their website with you own WhatsApp application. When you do this, the browser recognizes your phone and than makes sure that the connection between your phone and the web- whatsapp service is available.

In order to actually call the script we are going to send JSON data with PHP to the Chat-API web service in order to make it send a message. The token string and instance_id are findable on the Chat-API website after you connected your WhatsApp application to their Web- WhatsApp.

The phone number should contain the country number, which can be written in either 0031 or +31 for the Netherlands as example. 

Subscribe to our newsletter

Want to get frequent notifications about the posts on our blog? Fill in your details with the options we provide and then we will handle the rest for you!