PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 19, 2022

[FIXED] when we make call using rest api then what twiml we use on that url parameter

 November 19, 2022     twilio, twilio-api, twilio-click-to-call, twilio-php     No comments   

Issue

I am Creating the call using the rest api-

try{
        // Initiate a new outbound call
        $call = $this->client->calls->create(

            // to call.
            "num2",

            // Step 5: Change the 'From' number below to be a valid Twilio number 
            // that you've purchased or verified with Twilio.
            "num1",


            array("url" => "url-tw",
            'IfMachine'=>'Continue')

        );
        echo "Started call: " . $call->sid;
    } catch(Exception $e){
        echo "Error: " . $e->getMessage();
    }

and on the url-tw what twiml should I use which can't disconnect the call.

Before I was handling the call using the TwiML but now I have to detect the AnsweredBy option which is only available if I make the call using the REST API so.

for now I m using the same twiml I have used before when I was making calls using the twiML like use the <Dial> which let to dial again but if I dont use any twiml it disconnect the call.So any advice where I m going wrong.


Solution

Twilio evangelist here.

The value of the url parameter should be a publicly accessible URL that returns TwiML containing the instructions that you want Twilio to execute to the caller.

Your PHP to start the call would look like:

// Initiate a new outbound call
$call = $this->client->calls->create(
    "num2",
    "num1",
    array("url" => "http://example.com/answer.php", 'IfMachine'=>'Continue')
);

Then in answer.php, you can do two things:

  1. Check the AnsweredBy parameter to see if Twilio detected a machine or a human.
  2. Generate the Twiml you want to return based on that value

For example to say something different to a machine v a human, you could do something like this in your PHP:

<?php
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<?php if ($_REQUEST['AnsweredBy']=='machine'): ?>
    <Response>
        <Say>Hello machine</Say>
    </Response>
<?php else: ?>
    <Response>
        <Dial>+15555555555</Dial>
    </Response>
<?php endif ?>

Hope that helps.



Answered By - Devin Rader
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing