2016-04-15 11 views
0

我試圖使用Twilio API,這是在這裏:https://www.twilio.com/lookup使用PHP的捲曲度拉的數據和-XGET

它說,如果我把這叫做:

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

它將與迴應數據是這樣的:

{ 
"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"); 

但第一個不能正常工作。第二個工程,但它說我沒有通過一個有效的sid和身份驗證...

那麼,有沒有辦法讓這項工作沒有噸和噸的代碼?

回答

1

我剛剛不得不這樣做最近。它看起來像你試圖錯誤地傳遞Curl認證參數。給這個函數一試:

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(); 

} 

然後你只需通過你想查找這樣的數字:

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

你應該再得到這樣的迴應:

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 
) 

好運氣!

+0

工作。謝謝!! –