Prerequisites:
- Some version of PHP 5 installed, preferably the latest. Composer is compatible with PHP versions 5.3.2 and up.
- Install composer globally
Step 1: Set up a Project
Create a folder for your project:
mkdir sender
cd sender
Now create a composer configuration file and open it:
touch composer.json
open composer.json
Step 2: Install the SDK
Run the following command:
composer require messagemedia/messages-sdk
You will see the dependency appear in the composer configuration file you made earlier.
Step 3: Add the Code
Run the command:
touch sendMessage.php
open sendMessage.php
Paste the following code into open file:
$body->messages = array(); $body->messages[0] = new Models\Message; $body->messages[0]->content = 'My first message'; $body->messages[0]->destinationNumber = '+61466412529'; try { $result = $messagesController->sendMessages($body); print_r($result); } catch (Exceptions\SendMessages400Response $e) { echo 'Caught SendMessages400Response: ', $e->getMessage(), "\n"; } catch (MessageMediaMessagesLib\APIException $e) { echo 'Caught APIException: ', $e->getMessage(), "\n"; } ?> Replace the API key and Secret with your key and the phone number with your phone number.
Step 4: Send
Run the command:
php sendMessage.php
You should see the following print out in the screen:
You’ve now sent a simple message with PHP. Check out the API documentation and the Github repository to see what else you can do.
Comments
0 comments
Article is closed for comments.