Articles Blog

How to Make and Receive Phone Calls with PHP

How to Make and Receive Phone Calls with PHP

This PHP Voice Quickstart covers both inbound and outbound phone callse. But for both of those we need a phone number. So let’s go get one! We’ll start in the Console in the Phone Numbers section and I’ll search for a 267 area code phone number. I’ll buy the first Philadelphia phone number I find and we’ll set the number up later. Let’s start by talking about outbound calls. Your app makes a POST request to Twilio. Twilio places a call to your phone. When the call is connected, Twilio requests some TwiML instructions from your application. If the TwiML looks like this, then Twilio speaks some text back to your phone. Let’s write some code to make that work. We’ll start in the terminal by using Composer to install the Twilio PHP helper library. If you’re not familiar with Composer, it just helps us manage 3rd party dependencies in our app. Then we’ll create a PHP file to make our phone call. The first thing we’ll do in our PHP file is pull in the Twilio helper library that we just downloaded. And then we’ll declare that from it we want to use the Twilio REST client. To use the Twilio REST client we’re going to need some credentials: namely our Account Sid and Auth Token. from our Twilio account. These look a bit like this but I have these stored in environment variables and I recommend you do as well. So we’ll fetch the Account Sid and Auth Token and then we’ll use those to create a Twilio REST client passing them into the constructor. Now we’ll use the client to create an outbound phone call. So `client->calls->create` The first parameter that takes is the phone number to call. That’s gonna be my cell phone number and I’ll grab that out of my environment. Second parameter is the phone number that it’s going to come from and that’s our Twilio number we purchased earlier. The third parameter is an array of parameters And the only one we need to pass in is the URL that Twilio should request the TwiML instructions from when the call is connected. When the call connects, we’ll log out the Call Sid and just in case there’s an error we’ll log that out as well. The we can head back over to the terminal and make our call. Robot voice: Thanks for trying our documentation Enjoy! ♪ ♪ Rickroll ♪ ♪ ♪ ♪ Rickroll ♪ ♪ ♪ ♪ Rickroll ♪ ♪ Thanks Twilio. Who doesn’t love a Rickroll? Let’s check out the TwiML to see how that happened. There’s athat does text-to-speech and reads out “Thanks for trying our documentation. Enjoy!” and then atag that plays that lovely classic Rickroll MP3. Now let’s talk about inbound calls. A user places a phone call to your Twilio number. Twilio makes an HTTP request to your app which returns some TwiML and if the TwiML looks like this, then Twilio speaks some text back to the user. Let’s write the server code to make that happen. We’ll spin up our server.js file and We’ll start by creating a PHP file to hold the response for an incoming call. The first thing we’ll do inside of this PHP file is set the content-type header to text/xml since TwiML is just a set of plain XML instructions. We’ll create atag to respons to the incoming call and inside of thewe’ll create atag that does speech to text and it’ll say back to the caller: “Hello from your pals at Twilio. Have fun!” Now we’ll head back to the terminal and start a PHP server on localhost port 8000. There’s only one problem: Twilio can’t reach localhost:8000 publicly on the internet so we’ll use a tool called ngrok to create an HTTP tunnel on port 8000. This’ll give Twilio a publicly addressable URL it can reach when a call comes in. So we’ll copy the URL for this tunnel out and take it back over to the Twilio console and paste it into the “A call comes in” webhook location and add /incoming.php to it. We’ll set the HTTP method to GET since our PHP file will be accessible via an HTTP GET request. Then we can place a call to our phone number and get the response. Phone: “Hello from your pals at Twilio! Have fun” And I hope you will have fun now that you know how to handle both incoming and outgoing calls with PHP using Twilio. That’s gonna do it for this video. If you enjoyed this That’s gonna do it for this video. If you’ve enjoyed this please consider clicking Subscribe down below. If there’s something else you’d like us to cover leave us a comment down below and we’ll get right on it. In the meantime, check out one of these other videos to learn more about Twilio. Until next time, I’m outta here. ♪♪♪

13 thoughts on “How to Make and Receive Phone Calls with PHP”

  1. What about setting up Twilio to receive a Google voice verification code while you're on a trial account. For residents outside of USA.

  2. I am in dubai, here all voip calls are banned like skype, imo and whatsapp calling, with twilio is it possible to make an application like skype or something to have a video calling only with one person (in ex, to my wife ) using php ? if it is possible what are the ways to do it with your API and php ? else what are the other options available ?

  3. hi not sure if you are still active but any idea how to sort my error? when i run php make-calls.php i get this on my command prompt " PHP Warning: PHP Startup: Unable to load dynamic library 'C:/Program Files/NetMake/v9/components/phpextphp_ibm_db2.dll' – The specified module could not be found.
    in Unknown on line 0
    PHP Warning: PHP Startup: Unable to load dynamic library 'C:/Program Files/NetMake/v9/components/phpextphp_pdo_informix.dll' – The specified module could not be found.
    in Unknown on line 0
    Error: SSL certificate problem: unable to get local issuer certificate"

  4. Great video. Im confused on the process of how i can set my own custom message instead of the demo that twilio offers. Ive done the sms where i use ngrok to get a http url, is the process for setting outgoing calls the same?

  5. I want to talk directly instead of programmable voice (just like our mobile phone call). Is it possible with TWILIO

  6. Using Windows 7, php editor and localhost. I cannot get to step 1. I got a twillio account. I wrote the php, but cant even figure out where to get the "helper library". step 1 has me completely freaking lost.

  7. How can i get the phone number of incoming caller. I want to send a message to caller with another API for which i need the caller's phone number during call.

  8. Can you direct me to API where it could call directly to client .. for example
    i have an insurance and lender form when their form is filled out from the visitors.
    A call should go to visitor's number from the lender or insurance holder's number.

Leave a Reply

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