ckportfolio.com - Outbound Text Message via Twilio

Outbound Text Message via Twilio

Form Fields

Working to allow developers to easily interface with traditional communication methods including phone calls and text messages, Twilio provides a simple API service for us to send a text message to a specific number. We can first start with creating another <form> element based on the original search box, except this element will appear only if the $s variable is not empty:

share.php

<? if ($s != "") { ?>

    <form action="send.php" method="POST">

        <input name="search" type="hidden" value="<?= $s ?>">

        <div class="input-group mb-3">
            <input name="number" type="text" class="form-control" required placeholder="Recipient Number">
            <div class="input-group-append">
                <button class="btn btn-primary" type="submit">Send</button>
            </div>
        </div>

    </form>

<? } ?>

While loosely based on the previous search.php, the above snippet provides the following important distinctions:

  • <form> shows up only if $s is not empty
  • <form> now submits to the currently nonexistent send.php page, using POST method instead of GET
  • The original <input> element now has new name and placeholder attributes, and requires a value to proceed
  • A new hidden <input> element called "search" is added to pass the current search query to send.php

Text Message

Using the POST method discussed in cURL article, we can construct a POST call to the Twilio server. As Twilio APIs can be cumbersome to navigate at first, one can use the following sample code:

send.php

<?

    $phone = $_POST["number"];
    $body = "Check out this image list: http://ocad.ckprototype.com/public/wk4-ck/index.php?search=".$_POST["search"];
    $from = "+16477979348";

    $data = "To=".$phone."&From=".$from."&Body=$body";

    $headers = array(
        'Content-Type: application/x-www-form-urlencoded'
    );

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/AC2c7fe8f302ed82af71954c51cf918b17/Messages.json');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_USERPWD, 'AC2c7fe8f302ed82af71954c51cf918b17' . ':' . 'bb6bd5ee290ae35336fca3b92d727be7');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($curl);

    curl_close();

    header("Location: index.php?search".$_POST["search"]);

?>

We are making the necessary call to Twilio using the following steps:

  1. Construct various variables based on user-provided data (phone and body)
  2. Build a "payload" $data variable
  3. Initialize and configure cURL based on Twilio-provided requirements
  4. Make a call to Twilio and close the cURL instance
  5. Upon completion, redirect back to the original search results

From phone number, along with destination URL and user-password combo, have been created specifically for this tutorial.

If successful, the contents of the above body variable will be sent to the recipient's cell phone.

Fin