Reputation: 199
I am trying to get specific data from Twilio's Phone Validator curl command:
$cmd='curl -XGET "https://lookups.twilio.com/v1/PhoneNumbers/5551234321'?Type=carrier&Type=caller-name" -u "{AccountSid}:{AuthToken}" ';
exec($cmd,$result);
When I print_r the result, I get an array.
echo '<pre>'; print_r($result); '</pre>';
Array
(
[0] => {"caller_name": {"caller_name": "JOHN SMITH", "caller_type": "CONSUMER", "error_code": null}, "country_code": "US", "phone_number": "+5551234321", "national_format": "(555) 123-4321", "carrier": {"mobile_country_code": "310", "mobile_network_code": "120", "name": "Sprint Spectrum, L.P.", "type": "mobile", "error_code": null}, "add_ons": null, "url": "https://lookups.twilio.com/v1/PhoneNumbers/+5551234321?Type=carrier&Type=caller-name"}
)
How can I get specific values as PHP variables? e.g. "name": "Sprint Spectrum, L.P.", "type": "mobile" as:
$name = "Sprint Spectrum, L.P.";
$type = "mobile";
I tried:
foreach ($result->items as $item) {
var_dump($item->carrier->name);
}
But get an error: Invalid argument supplied for foreach()
Upvotes: 0
Views: 275
Reputation: 199
Thank you @EatPeanutButter and @Julqas for your assistance. Pointed me in the right direction.
Working code:
$json = json_decode($result[0],true);
$type = $json['carrier']['type'];
$carrier = $json['carrier']['name'];
Upvotes: 1