mckaymental
mckaymental

Reputation: 33

Twilio API - Can't assign phone number to Messaging Service

I'm trying to assign a newly created phone number to an existing Messaging Service from the Twilio API. Here's my code so far:

Creating the Messaging Service

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

$messagingServiceId = $createService->sid;

Creating the phone number and assigning to the Messaging Service

$smsNumber = $_POST["smsNumber"];

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

With the previous code, I'm able Messaging Service is created, the phone number is added to the account but it is not assigned to the Messaging Service.

What am I missing here?

Thanks for your help.

Upvotes: 0

Views: 543

Answers (1)

Alan
Alan

Reputation: 10771

Look at the Create A Phone Number code example for the PhoneNumber Resource.

<?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);

Upvotes: 1

Related Questions