You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Twilio PHP/JavaScript SDK实现呼叫保持时主叫方掉线问题排查

问题描述

我正尝试通过Twilio JavaScript SDK与PHP实现语音呼叫的保持功能,但在尝试将呼叫的两端(主叫方与被叫方)同时置为保持状态时,出现仅被叫方成功保持、主叫方呼叫掉线的问题。调用保持函数时,主叫方会被断开连接,当前代码仅对被叫方生效。请帮忙指出需要修正的地方,谢谢!

相关代码

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 => "https://api.twilio.com/2010-04-01/Accounts/Account 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 => "https://api.twilio.com/2010-04-01/Accounts/Account 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 => "https://api.twilio.com/2010-04-01/Accounts/Account 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);  

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>http://com.twilio.sounds.music.s3.amazonaws.com/MARKOVICHAMP-Borghestral.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"
}

问题详情

使用上述代码时,仅被叫方(child leg)可被成功置为保持状态,而主叫方(parent leg)的呼叫会直接断开。我需要实现呼叫两端同时保持的功能,但当前未达到预期效果。


内容的提问来源于stack exchange,提问作者Ryan Naong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:55:54