Reputation: 11
I am looking for a little assistance on listing available phone numbers for purchase using Twilios API and PHP for their 5.X API Verison. Below is the error I get and the PHP im using. Im sure im just overlooking something:
PHP Notice: Trying to get property of non-object in /twilio-php-app/findnumbers.php on line 16 PHP Warning: Invalid argument supplied for foreach() in /twilio-php-app/findnumbers.php on line 16
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "Axxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$token = "removed";
$client = new Client($sid, $token);
$numbers = $client->availablePhoneNumbers('US')->local->read(
array("areaCode" => "513")
);
foreach($numbers->availablephonenumbers as $number) {
echo $number->phone_number;
}
If I echo $numbers I find it is an array. Here is the raw output where I just want to get the "phone_number": "xxxxxx" output; minus the "phone_number": part.
Adding to this, if I run the PHP as the following; I get single number outputs
$numbers = $client->availablePhoneNumbers('US')->local->read(
array("areaCode" => "513")
);
echo $numbers[1]->phoneNumber;
Changing the value of [1] to [2] grabs the next phone number. How can I loop this?
Upvotes: 0
Views: 908
Reputation: 11
Might not be done 100% correct but I found a solution that increases the count of the array based on count and stacks the numbers nicely.
Sharing this in case anyone else ever comes across this and needs help; this does exactly what its intended to.... Search the twilio databse for available numbers to purchase, based on criteria
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "your_SID";
$token = "Your_Token";
$client = new Client($sid, $token);
$numbers = $client->availablePhoneNumbers('US')->local->read(
array("areaCode" => "513")
);
for ($i = 0; $i < count($numbers); ++$i) {
print $numbers[$i]->phoneNumber . "\n";
}
Upvotes: 1
Reputation: 44
Just an observation, but you are already using the availablePhoneNumbers method on $client when you say $numbers = $client->availablePhoneNumbers...
Perhaps, in the foreach, you just need to reference $numbers
and not $numbers->availablephonenumbers
?
Upvotes: -1