Looking for an awesome Twilio API tutorial? This probably isn’t it but feel free to continue…
What is Twilio?
Twilio is a company that provides voice and SMS services for your application or website.
What do I need to start?
What am I missing?
This tutorial is not on the process of setting up a server, so I will be skipping those parts right now. I also expect that you know PHP already and can implement basic OOP applications.
Easy Twilio API starts here
First, create a composer.json manually or using “composer init” in the command line. Your file’s contents should look something like this:
{ "name": "dj/sms", "license": "MIT", "authors": [ { "name": "Randi Miller", "email": "randi@devjunkies.net" } ], "require": { "twilio/sdk": "*" } }
Now that your composer.json file is setup, run the “composer install” command to install the vendor packages. Create a file named sms.php in the same directory level as composer.json.
Inside the file you will need to require the composer autoload.php to add the Twilio SDK to the namespace.
require('vendor/autoload.php');
Then you want to add your Twilio SID and token so that Twilio knows what person is using their API.
$sid = 'Enter SID here'; $token = 'Enter token here';
Now we can initialize the Services_Twilio object.
$twilio = new Services_Twilio($sid, $token);
Using your object, you can now create an SMS message and send it to a loved one <3
$twilio->account->messages->create(array( 'To' => "Loved one's Phone #", 'From' => "Twilio Phone #", 'Body' => "The message you want to send goes here!!!", ));
Just run the sms.php script and BAMMM! You have SMS abilities. Now what will you do since you are the best Twilio developer?
Hi,
There is a bug in your example, it should be:
$twilio->account->messages->create…
Not:
$client->account->messages->create