Twillio Php api

时间:2016-08-08 02:03:37

标签: php twilio

有 我使用Twillio示例api     

// Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
// and move it into the folder containing this sendnotifications.php file.
require "Services/Twilio.php";

// Step 2: set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "ACea465f2e60ae87332cf47adb0e4aad64";
$AuthToken = "your_auth_token";

// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);

// Step 4: make an array of people we know, to send them a message. 
// Feel free to change/add your own phone number and name here.
$people = array(
    "+15558675309" => "Curious George",
    "+15558675308" => "Boots",
    "+15558675307" => "Virgil",
);

// Step 5: Loop over all our friends. $number is a phone number above, and 
// $name is the name next to it
foreach ($people as $number => $name) {

    $sms = $client->account->messages->sendMessage(

        // Step 6: Change the 'From' number below to be a valid Twilio number 
        // that you've purchased
        "+15017250604", 

        // the number we are sending to - Any phone number
        $number,

        // the sms body
        "Hey $name, Monkey Party at 6PM. Bring Bananas!"
    );

    // Display a confirmation message on the screen
    echo "Sent message to $name";
}

这是我的代码     

// Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
// and move it into the folder containing this sendnotifications.php file.
require "Services/Twilio.php";

// Step 2: set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "accountsid";
$AuthToken = "authtoken";

// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);

// Step 4: make an array of people we know, to send them a message. 
// Feel free to change/add your own phone number and name here.
$people = array(
    "my-number-goes-here" => "Ali M. Ayad",

);

// Step 5: Loop over all our friends. $number is a phone number above, and 
// $name is the name next to it
foreach ($people as $number => $name) {

    $sms = $client->account->messages->sendMessage(

        // Step 6: Change the 'From' number below to be a valid Twilio number 
        // that you've purchased
        "+13862593325", 

        // the number we are sending to - Any phone number
        $number,

        // the sms body
        "Hey Ali This is test message form the api"
    );
    // Display a confirmation message on the screen
    echo "Sent message to $name";
}

当我输入此代码并且我更改了身份验证和身份证号码时,该号码是我的此消息

  

致命错误:未捕获的异常' Services_Twilio_RestException'消息'身份验证错误 - 用户名无效'在C:\ AppServ \ www \ tw \ Services \ Twilio.php:297堆栈跟踪:#0 C:\ AppServ \ www \ tw \ Services \ Twilio.php(180):Base_Services_Twilio-> _processResponse(Array)#1 C:\ AppServ \ www \ tw \ Services \ Twilio \ ListResource.php(92):Base_Services_Twilio-> createData(' / 2010-04-01 / Acc ...',Array)#2 C:\ AppServ \ www \ tw \ Services \ Twilio \ Rest \ Messages.php(24):Services_Twilio_ListResource-> _create(Array)#3 C:\ AppServ \ www \ tw \ Services \ Twilio \ Rest \ Messages.php (71):Services_Twilio_Rest_Messages-> create(Array)#4 C:\ AppServ \ www \ tw \ sendnotifications.php(48):Services_Twilio_Rest_Messages-> sendMessage(' + 13862593325',' + 201063378955',' Hey Ali这是......')#5 {main}在第297行的C:\ AppServ \ www \ tw \ Services \ Twilio.php中抛出    我正在使用appserv

2 个答案:

答案 0 :(得分:1)

正如错误消息(几乎)清楚地说:

  

身份验证错误 - 用户名无效

假设您已经在twilio注册;确保在您使用的真实代码中正确定义了用户名和令牌:

  • 一次又一次地重新检查您的用户名(以及AuthToken),以了解可能的编码问题和/或错误,包括:
    • 正确匹配字符大小写,
    • 尾随空格和
    • 特殊字符。

同时确认您的帐户在twilio仍然有效。

答案 1 :(得分:0)

“身份验证错误 - 用户名无效”

您的AccountSid和AuthToken是您帐户的“主密钥”。要使用这些“主密钥”进行身份验证,请使用HTTP基本身份验证,并将用户名设置为您的AccountSid,并将密码设置为您的AuthToken。您可以在帐户信息中心找到您的AccountSid和AuthToken。