2016-08-16 8 views
0

を使用して、電話機の種類やキャリアを取得し、私はTwilioの電話バリcurlコマンドから特定のデータを取得しようとしています:私は結果をしますprint_rときTwilio電話バリカールコマンド

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

、私は配列を取得します。

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変数として取得するにはどうすればよいですか?例: "名前": "スプリントスペクトラム、LP"、 "タイプ": "モバイル" として:私が試した

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

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

しかし、エラーが発生します:foreachのために供給無効な引数()

+2

[PHPでJSON配列から値を取得](http://stackoverflow.com/questions/17995877/get-value-from-json-array-in-の可能性のある重複PHP) – WillardSolutions

+0

あなたの結果はJSONエンコードされています。 json_decode関数を使用して$ result [0]をjson_decodeする必要があります(例については、EatPeanutButterのリンクを参照してください) – Julqas

+0

[Twilio PHPライブラリ](https://www.twilio.com/docs/libraries/)を試してみることをお勧めしますか? PHP)。それはあなたのためにすべてを扱います(あなたがカールするために外に出る必要なしにコールを行います)。 – philnash

答えて

1

@EatPeanutButterと@Julqasありがとうございました。私を正しい方向に向ける。

の作業コード:

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

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

$carrier = $json['carrier']['name']; 
関連する問題