Send SMS Using Twilio SMS API in PHP

In this tutorial, we will introduce the way to send sms using php with Twilio SMS API.

1. You should create an account in Twilio first, then you can use Twilio SMS API to send sms in php.

2. Create a php function to send sms

function send_twilio_sms($id, $token, $from, $to, $body)
{
$url = "https://api.twilio.com/2010-04-01/Accounts/".$id."/SMS/Messages";
$data = array (
    'From' => $from,
    'To' => $to,
    'Body' => $body,
);
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
curl_close($x);
return $y;
}
?>

In this php function, you should check your Twilio account and token.

3. You can use this function like:

send_twilio_sms($id, $token, $from, $to, $body)
Posted Under