2

I am making a PHP website I am trying to send a message using Twilio whenever the user enters the telephone number in the website. Currently it only sends a message to the number I wrote in my code. How can I make it send to the entered number?

<?php
 require('Twilio.php');
// ==== Control Vars =======
$strFromNumber = "";
$strToNumber = "";
$strMsg = "Hey!! ";  
$aryResponse = array();

    // set our AccountSid and AuthToken - from www.twilio.com/user/account
    $AccountSid = "";
    $AuthToken = "";


    // ini a new Twilio Rest Client
    $objConnection = new Services_Twilio($AccountSid, $AuthToken);


    // Send a new outgoinging SMS by POSTing to the SMS resource

    $bSuccess = $objConnection->account->sms_messages->create(

        $strFromNumber,     // number we are sending from 
        $strToNumber,           // number we are sending to
        $strMsg         // the sms body
    );



    $aryResponse["SentMsg"] = $strMsg;
    $aryResponse["Success"] = true;

    echo json_encode($aryResponse);
?>
mrry
  • 125,488
  • 26
  • 399
  • 400

1 Answers1

2

Set $strToNumber to what ever number the person types in. You can get this on a form submission.

If you use a post request, you can send the user to a php page that gets the number:

Example form:

<form action="[path to your php file].php" method="post">
    Phone Number: <input type="tel" name="phone"><br>
    <input type="submit">
</form>

In [path to your php file].php, put the code you had above, and set $strToNumber to $_POST["number"]:

$strToNumber = $_POST["number"];

Twilio also charges money when you send a message, and if you are on a trail account, you might not be able to send a message.

Ben Aubin
  • 5,542
  • 2
  • 34
  • 54