In this SMS API Quickstart, you'll learn how to send an SMS from your web application using the SMS API.
To start testing with the official PHP SDK for the SMS API, you need to first set up your test environment.
The following command will install MessageBird:
composer require messagebird/php-rest-api
Now, let's create a send_sms.php file in the directory of your composer.json file
Want to learn how to set up your Python development environment? Check out the PHP tutorial
Next, enter your credentials and mobile number - as both the originator and recipient - to the snippet below and copy it to your send_sms.php file
<?phprequire_once __DIR__.'/vendor/autoload.php';$messagebird = new MessageBird\Client('Your-API-Key]');$message = new MessageBird\Objects\Message;$message->originator = '+31XXXXXXXXX';$message->recipients = [ '+31XXXXXXXXX' ];$message->body = 'Hi! This is your first message';$response = $messagebird->messages->create($message);var_dump($response);
You can access and manage your credentials in the MessageBird Developer Dashboard.
Now, run the following command line:
php send_sms.php
Good job! You’ve just sent your first SMS with the MessageBird SMS API!
Now, let’s view MessageBird’s HTTP response to your API call. If the set up was correct, you'll receive a response similar to this:
{id: '8144cda2abc84795a306c24b62f46a72',href: 'https://rest.messagebird.com/messages/8144cda2abc84795a306c24b62f46a72',direction: 'mt',type: 'sms',originator: '+31XXXXXXXXX',body: 'Hi! This is your first message',reference: null,validity: null,gateway: 10,typeDetails: {},datacoding: 'plain',mclass: 1,scheduledDatetime: null,createdDatetime: '2019-02-19T12:21:08+00:00',recipients:{totalCount: 1,totalSentCount: 1,totalDeliveredCount: 0,totalDeliveryFailedCount: 0,items: [ [Object] ] }}
If something went wrong, you’ll receive an error response from the MessageBird API. Don't worry, simply head over to the SMS API Troubleshooting to understand the error.
Congrats! You can now programmatically send SMS with the MessageBird SMS API using PHP.