0

我正在尝试从 Twilio 的电话验证器 curl 命令中获取特定数据:

$cmd='curl -XGET "https://lookups.twilio.com/v1/PhoneNumbers/5551234321'?Type=carrier&Type=caller-name" -u "{AccountSid}:{AuthToken}" ';
exec($cmd,$result);

当我 print_r 结果时,我得到一个数组。

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"}
)

如何获取特定值作为 PHP 变量?例如“名称”:“Sprint Spectrum,LP”,“类型”:“移动”为:

$name = "Sprint Spectrum, L.P."; 
$type = "mobile";

我试过了:

foreach ($result->items as $item) {
   var_dump($item->carrier->name);
}

但是得到一个错误:为 foreach() 提供的参数无效

4

1 回答 1

1

感谢@EatPeanutButter 和@Julqas 的帮助。为我指明了正确的方向。

工作代码:

$json = json_decode($result[0],true);

$type = $json['carrier']['type'];

$carrier = $json['carrier']['name'];
于 2016-08-16T14:23:02.977 回答