Send SMS to Phone Number using Twilio in PHP

Send SMS using Twilio in PHP

In this PHP snippets post, we are going to find a simple way to send SMS using Twilio in PHP. Here you will see how easily you can send SMS to a phone number using Twilio API.

Before we go forward, let me tell you about Twilio in brief.

Twilio is a cloud communications platform as a service company that is based in San Francisco, California. It provides their services through API. SMS is one of the popular services of Twilio that can allow sending SMS to phone number from website or app.


Also, read:


Twilio allow sending both text SMS and SMS with OTP to verify a phone number. Here in this post, we are going to see how to send simple text SMS to a phone number using Twilio SMS API.


PHP function to send text SMS using Twilio API

Below is our PHP code snippets to create the function with the required parameters:


function send_twilio_text_sms($id, $token, $from, $to, $body)

$url = "".$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_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
return $y;


We have just created our function. We can use it to send an SMS to a phone number.

Below is how to use this PHP function:


In the above code, you can see that we just have to pass 5 parameters into the function. These are listed below:

  • Twilio SID – After you create your account on Twilio you can create your project and get the Twilio SID.
  • Twilio Token – You will also get the token after you create the project on Twilio that you have to pass to the function we have just created.
  • Sender phone number – You have to create a sender phone number and put it in the function.
  • Receiver phone number – This is the phone number where you have to send SMS.
  • SMS text is the text that you want to send to the sender phone number.

That’s it. After creating the function, you just have to call it and with all the required parameters.


Leave a Reply

Your email address will not be published. Required fields are marked *