Twilio API-无法将电话号码分配给消息服务

时间:2020-04-11 22:32:37

标签: twilio twilio-api twilio-php

我正在尝试通过Twilio API将新创建的电话号码分配给现有的消息服务。 到目前为止,这是我的代码:

创建消息服务

$createService = $twilio->messaging->v1->services
  ->create([
    "friendlyName" => "Service",
    "inboundRequestUrl" => "https://example.com/folder/file.php"
  ]
);

$messagingServiceId = $createService->sid;

创建电话号码并分配给消息服务

$smsNumber = $_POST["smsNumber"];

$addPhoneNumber = $twilio->incomingPhoneNumbers
  ->create([
    "phoneNumber" => $smsNumber,
    "smsApplicationSid" => $messagingServiceId
  ]
);

使用前面的代码,我能够创建Messaging Service,电话号码已添加到帐户,但未分配给Messaging Service。

我在这里想念什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

查看电话号码资源的Create A Phone Number代码示例。

<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';

use Twilio\Rest\Client;

// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid    = "ACCOUNT_SID";
$token  = "your_auth_token";
$twilio = new Client($sid, $token);

$phone_number = $twilio->messaging->v1->services("MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                                      ->phoneNumbers
                                      ->create("PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // phoneNumberSid
                                      );

print($phone_number->sid);