PHP Example

PHP is a commonly used server-side language for websites.

Its worth looking at the HTML tutorial. The basics of API integration is simply POSTing parameters to and from the endpoint.

Because PHP is server-side your username and password are not exposed, however we do recommend you carefully consider the security around storing them as plaintext within the PHP file.

This part of the Tutorial covers sending messages. See the PHP Receiving Tutorial to have replies sent back to your application.

Plan of attack

This example is a little more involved than the HTML Send Tutorial.

We will create a PHP page that integrates the HTML form created earlier with the code to send messages to the Messenger API.

A common scenario is if you have a page within a site/portal or otherwise and want to send text messages through the Messenger API, with the passwords inside the PHP script, it would be secure from end users. It also allows you to perform other activities such as updating a user's message balance.

What we'll do therefore is:

  • Write the code
  • Place the code onto a PHP server
  • Send a message
  • Have a pat on the back - you've integrated the Messenger API with a dynamic PHP site

Write the code

Open up a text editor and paste the code below:


/* Constants */
$smsUser = 'yourusernamehere';
$smsPass = 'yourpasswordhere';
$smsURL = '';

/* Request parameters */
$phone = $_REQUEST['mobile'];
$request = $_REQUEST['body'];

/* Generate a reply - this is where you would hit the database */
$reply_id = 1;
$reply = "Hello $phone. I heard you say: $request";

/* Send the reply */
sendSMS($smsUser, $smsPass, $reply_id, $phone, $reply);

/* returns TRUE on success, FALSE on failure */
function sendSMS($user, $pass, $msgid, $recipient, $body) {
    global $smsURL;

  $data = "userId=".urlencode($user)
    . "&password=".urlencode($pass)
    . "&messageId=".urlencode($msgid)
    . "&to=".urlencode($recipient)
    . "&body=".urlencode($body);

  $cUrl = curl_init();
  curl_setopt($cUrl, CURLOPT_URL, $smsURL);
  curl_setopt($cUrl, CURLOPT_HEADER, 'Content-type: application/x-www-form-urlencoded');
  curl_setopt($cUrl, CURLOPT_POST, 1);
  curl_setopt($cUrl, CURLOPT_POSTFIELDS, $data);
  curl_setopt($cUrl, CURLOPT_TIMEOUT, 30);

  $code = curl_getinfo($cUrl, CURLINFO_HTTP_CODE);


  return ($code == 200 || $code == 204) ? TRUE : FALSE;


Footnote: SMS.php

Save the file as SMS.php

Prepare some test HTML Open up a text editor and paste the code below:

<!-- URL for SENDING messages only must use POST method-->
<form name="sendSMS" action="" method="post">

<!--Enter recipient in INTERNATIONAL format-->
   <input value="[number INTL format]" name="mobile">

<!--Message BODY will be form ENCODED-->
   <textarea name="body" rows="4" cols="30">
      [Message Content]

   <input type="reset" value="Clear">
   <input type="submit" value="Send">

Footnote: SMS.html

Save the file as SMS.html

This form has a basic input for a phone number, a text box for the message content with submit and clear buttons. When the form is submitted it sends to your PHP code at which then submits your SMS message to the recipient

Replies are not handled by this example.

Deploy the code

Copy SMS.php to a suitable location on your PHP-compatible Web server and access the configured URL in your browser.

End to end Test

Visit SMS.html on your server, complete the form fields and click Send

Your message should be delivered to your handset.