With these instructions we are letting Composer know that this project requires PHP version 7.1 or greater, the Twilio SDK for PHP, and the vlucas/phpdotenv project which we will use to store information we don't want to save in a public repo (more on this later). From inside our new project directory, we'll run the following command to have composer follow these instructions:
composer install
Setting Up the Twilio API Sandbox for WhatsApp
Currently, only approved business accounts are cleared by WhatsApp for access through the Twilio API, but we can use the Twilio API Sandbox for WhatsApp to start experimenting.
Before we get started we'll need to do a bit of setup. We'll head over to the Sandbox in our Twilio console which is located under the Programmable SMS menu. To activate our sandbox we'll select one of the Sandbox numbers, click to agree to WhatsApp terms, and click "Activate Sandbox". In the next section we find a couple of options for linking our personal WhatsApp account to the Sandbox.
Once our personal WhatsApp account is linked we can play around using the template forms in the Sandbox, but we're going to create a couple of PHP scripts to interact with the Twilio API for WhatsApp. We'll want to come back to this page later to add our app's ngrok URL to the "Connect the Sandbox to your App" section, so we'll leave it open for now.
Sending a Template Message
There are two ways to send an outgoing WhatsApp message through the Sandbox. Either we can reply when our WhatsApp number messages us, or we can send out a message that matches one of the available templates.
For our first experiment we will be sending a message to our WhatsApp number using a template message. We don't need to use the forms in the console as long as the Body of our message matches one of the template with the {{}} replaced with real data.
In our project directory we'll create a new file, sendMessage.php
, with the following content: