The following code was tested with test credentials and worked as expected, but when attempting to purchase a number with the production credentials I always get [HTTP 400] Unable to create record: +xxxxxxx is not available
Below is the code that retrieves the available numbers.
public function getAvailablePhoneNumbers(): array
{
if ($this->useTestClient) {
return [self::VALID_TEST_MAGIC_PHONE];
}
$numbers = $this->getClient()
->availablePhoneNumbers('US')
->local
->read([
'smsEnabled' => true,
]);
/* @var LocalInstance $number */
return array_map(function ($number) {
return $number->phoneNumber;
}, $numbers);
}
After retrieving the list of phoneNumber I try to acquire a number in the list or pass to the next in the list if it fails, all the numbers on the list fail with the same error, the code that tries to acquire it is.
public function purchaseNumber(string $number): \Twilio\Rest\Api\V2010\Account\IncomingPhoneNumber\LocalInstance
{
return $this->getClient()
->incomingPhoneNumbers
->local
->create($number, [
'SmsApplicationSid' => config('twilio.sms_app_sid'),
]);
}
Edit: I tried to acquire one of the unavailable numbers form the log in the Twilio console and it succeeded.
I am not super familiar with PHP, but according to the docs, the syntax is:
Try using the example code with a known available phone number - including the proper "phoneNumber" reference. If it works, then the problem is within your getAvailablePhoneNumbers() or how you are referencing your $number array.