2016-04-15 10 views
0

、それはここにある:私はTwilioのAPIを使用しようとしていますカールでデータを引っ張ってPHPを使用して-XGET

curl -XGET "https://lookups.twilio.com/v1/PhoneNumbers/(405)%20555-1212?Type=carrier&Type=caller-name" -u "{AccountSid}:{AuthToken}" 

それがで応答します:https://www.twilio.com/lookup

それは私がこれを呼び出した場合と言います次のようなデータ:

{ 
"country_code": "US", 
"phone_number": "+14055551212", 
"national_format": "(405) 555-1212", 
"url": "https://lookups.twilio.com/v1/PhoneNumber/+14055551212", 
"caller_name": { 
    "caller_name": null, 
    "caller_type": null, 
    "error_code": null, 
}, "carrier": { 
    "type": "mobile", 
    "error_code": null, 
    "mobile_network_code": null, 
    "mobile_country_code": "310", 
    "name": null 
} 
} 

PHPではどのように変数として呼びますか?

$res = curl -XGET "https://lookups.twilio.com/v1/PhoneNumbers/(405)%20555-1212?Type=carrier&Type=caller-name" -u "{AccountSid}:{AuthToken}"; 

しかし、私はエラーを取得:

は、私はこれを試してみました。

私はこのようなカールの機能を作成しようとしました:

function url_get_contents($_turl) { 
    if (!function_exists('curl_init')) { 
     die('CURL is not installed!'); 
    } 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $_turl); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $output = curl_exec($ch); 
    if ($output === false) { die(curl_error($ch)); } 
    curl_close($ch); 
    return $output; 
} 

その後、これらのようにそれを呼び出す:

$res = url_get_contents("https://lookups.twilio.com/v1/PhoneNumbers/4055551212?Type=carrier&Type=caller-name -u AccountSID:AccountAuth"); 

$res = url_get_contents("https://lookups.twilio.com/v1/PhoneNumbers/4055551212?Type=carrier&Type=caller-name"); 

が、最初のものは動作しません。 2番目の作品は動作しますが、私は有効なSIDと認証を渡さなかったと言います...

だから、たくさんのコードなしでこの作業をする方法はありますか?

答えて

1

私はちょうどこれを最近しなければなりませんでした。 Curl認証パラメータを間違って渡しているようです。

Array 
(
    [caller_name] => Array 
     (
      [caller_name] => CLOUDFLARE, INC 
      [caller_type] => BUSINESS 
      [error_code] => 
     ) 

    [country_code] => US 
    [phone_number] => +16503198930 
    [national_format] => (650) 319-8930 
    [carrier] => Array 
     (
      [mobile_country_code] => 
      [mobile_network_code] => 
      [name] => Proximiti Mobility 2 
      [type] => voip 
      [error_code] => 
     ) 

    [url] => https://lookups.twilio.com/v1/PhoneNumbers/+16503198930?Type=carrier&Type=caller-name 
) 

グッド:

$this->lookup("650-319-8930"); 

あなたは、このような応答を取得する必要があります。

その後
function lookup($number){ 

    // Twilio Account Info 
    $acct_sid = "xxxxx"; 
    $auth_token = "yyyyy"; 

    // Fetch the Lookup data 
    $curl = curl_init("https://lookups.twilio.com/v1/PhoneNumbers/".$number); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($curl, CURLOPT_USERPWD, $acct_sid.":".$auth_token); 
    $response_data = curl_exec($curl); 
    curl_close($curl); 

    // Handle the Data 
    $lookup_data_array = json_decode($response_data,true); // Convert to array 
    print_r($lookup_data_array);die(); 

} 

あなたはちょうどあなたがこのようにルックアップしたい番号を渡します。この機能を試してみます運!

+0

これはうまくいきました。ありがとうございました!! –

関連する問題