我有同样的问题,
您需要在调用allowClientOutgoing() 和allowClientIncoming( ) 之后调用函数generateToken( ) 以便Services_Twilio_Capability() 创建的对象具有应用链接。
这有效:
$objToken = new Services_Twilio_Capability($accountSid, $authToken);
$objToken->allowClientOutgoing('APXXXXXXXXXX');
$objToken->allowClientIncoming($_REQUEST['name']);
$strToken = $objToken->generateToken();
这不会:
$objToken = new Services_Twilio_Capability($accountSid, $authToken);
$strToken = $objToken->generateToken();
$objToken->allowClientOutgoing('APXXXXXXXXXX');
$objToken->allowClientIncoming($_REQUEST['name']);
此外,它不会抛出错误,但您的 js 将始终显示为“断开连接”
更新
这是我的后端的编辑:
/**
* Create an instance of Services_Twilio_Capability();
*
* @return object
*/
private function instantiateCapability(){
if(is_null($this->objCapability))
$this->objCapability = new \Services_Twilio_Capability(TWILIO_ID, TWILIO_KEY);
return $this->objCapability;
}
/**
* Generate a token
*
* @link http://twilio-php.readthedocs.org/en/latest/usage/token-generation.html
* @param bool $boolOutgoing Allow outgoing connections
* @param bool $boolIncoming Allow incoming connections
* @return string
*/
public function generateToken($boolOutgoing = true, $boolIncoming = true){
$objCapability = $this->instantiateCapability();
if($boolOutgoing) $objCapability->allowClientOutgoing(TWILIO_SID]);
if($boolIncoming) $objCapability >allowClientIncoming($_SESSION[$GLOBALS['APP_NAME'] . 'ID']);
$strToken = $objCapability->generateToken(TOKEN_DURATION);
return json_encode(array('status' => 1, 'token' => $strToken));
}
这是前端(AJAX 响应回调):
function(result){
if(result.status == 1) {
//Load the twilio object
Twilio.Device.setup(result.token);
}
}