最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Put caller on hold using PHP javascript SDK in twilio - Stack Overflow

programmeradmin3浏览0评论

I am trying to implement a hold functionality using the Twilio JavaScript SDK for voice calls and PHP. However, when I attempt to put both legs of the call on hold, I encounter an issue where one leg is successfully placed on hold, but the other leg gets dropped.

Basically, Whenever I call the hold function, the caller is getting disconnected. The code described below only applies to the callee not the caller.

I have shared my code below. Can anyone please take a look and let me know what corrections I need to make? Thank you!

Call_hold.php

$jsonn = file_get_contents('../outgoing1.txt')  
$dataaaa = json_decode($jsonn, true)  
$child_leg = $dataaaa['CallSid'];  
$parent_id = $dataaaa['ParentCallSid'];

/*----- Modify the person I called child leg sent to wait music -----*/

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => " SID/Calls/$child_leg.json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_HTTPHEADER => array(
    'Authorization: Basic xxxxxxxxxxxxxxx',
    'Content-Type: application/x-www-form-urlencoded'
  ),
));

$response = curl_exec($curl);
$data = json_decode($response, true);
$ssid_id = $data['sid'];`

`$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => " SID/Calls/$ssid_id.json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'Method=POST&Url=outbound_customer_queue.php',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Basic xxxxxxxxxxxxxxx',
    'Content-Type: application/x-www-form-urlencoded'
  ),
));

$response = curl_exec($curl);`

`/* ------- The Caller(me) ParentCallSid sent to waiting hold music ---------- */
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => " SID/Calls/$paren_id.json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'Method=POST&Url=outbound_agent_queue.php',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Basic xxxxxxxxxxxxxxx',
    'Content-Type: application/x-www-form-urlencoded'
  ),
));

$response = curl_exec($curl);  

XML:

outbound_customer_queue.php

echo '<?xml version="1.0" encoding="UTF-8" ?>  
<Response>  
    <Say>Listen, please. Our agent team is very busy at the moment, and the agent you are talking to has put your call on hold. Please wait until the call is connected back to them. Thank you.</Say>  
    <Enqueue waitUrl="wait-music.php">supporting</Enqueue>  
    <Redirect>wait-music</Redirect>  
</Response>';  

wait-music.php

echo '<?xml version="1.0" encoding="UTF-8" ?>';  
echo "<Response>";  
echo "<Say>Hey, you are caller number ".$_REQUEST['QueuePosition'].". There is an average wait time of ".$_REQUEST['AvgQueueTime']."</Say>";  
echo "<Play>.mp3</Play>";  
echo "</Response>";  

outbound_agent_queue.php

echo '<?xml version="1.0" encoding="UTF-8" ?>  
<Response>  
    <Say>You have successfully placed the call on hold. Please be aware that the customer is waiting, and they will be returned to you when you connect back.</Say>  
    <Pause length="5"></Pause>  
    <Enqueue waitUrl="wait-music.php">supporting</Enqueue>  
    <Redirect>wait-music</Redirect>  
</Response>';  

outgoing1.txt

{
  "Called": "+16789835670",
  "ParentCallSid": "CA5218906a7d0574ec3cc351b29fa51362",
  "ToState": "Arizona",
  "CallerCountry": "US",
  "Direction": "outbound-dial",
  "Timestamp": "Fri, 14 Mar 2025 13:47:33 +0000",
  "CallbackSource": "call-progress-events",
  "SipResponseCode": "200",
  "CallerState": "PA",
  "ToZip": "",
  "SequenceNumber": "3",
  "CallSid": "CAc2fe08c722becd59574c3e0b99b025cf",
  "To": "+16789835670",
  "CallerZip": "",
  "ToCountry": "US",
  "CalledZip": "",
  "ApiVersion": "2010-04-01",
  "CalledCity": "",
  "CallStatus": "completed",
  "Duration": "1",
  "From": "+18145593276",
  "CallDuration": "32",
  "AccountSid": "******",
  "CalledCountry": "US",
  "CallerCity": "",
  "ToCity": "",
  "FromCountry": "US",
  "FromCity": "",
  "CalledState": "Arizona",
  "FromZip": "",
  "FromState": "AZ"
}

Issue:

When I try to use the above code, it only places the person I called (child leg) on hold while the call to the caller (me, the parent leg) gets disconnected, which makes the outbound call to that person. I want both legs of the call (caller and callee) to be placed on hold simultaneously, but it doesn’t seem to be working as expected.

发布评论

评论列表(0)

  1. 暂无评论