我在PHP文件中调用变量时出错Twilio发送消息

时间:2018-05-24 19:43:16

标签: php html sms twilio twilio-api

我正在按照website

中的说明配置Twilio sendnotifications.php文件

当我使用此代码在终端中启动“php sendnotifications.php”时,一切正常,我收到了我的短信

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'My id';
$token = 'My token';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    'MyPhoneNumber',
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => 'MyTwilioSendingNumber',
        // the body of the text message you'd like to send
        'body' => "My message"
    )
);

但是当我像这样运行时,我有一个错误:

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'My id';
$token = 'My token';
$client = new Client($sid, $token);
$phone='MyPhoneNumber';

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    $phone,
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => 'MyTwilioSendingNumber',
        // the body of the text message you'd like to send
        'body' => "My message"
    )
);

错误是:

MacBook-Pro:Test envoi sms accueil Joris$ php sendnotifications.php

Fatal error: Uncaught Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: The 'To' number 74663 is not a valid phone number. in /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Version.php:85
Stack trace:
#0 /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Version.php(219): Twilio\Version->exception(Object(Twilio\Http\Response), 'Unable to creat...')
#1 /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Rest/Api/V2010/Account/MessageList.php(69): Twilio\Version->create('POST', '/Accounts/AC8ec...', Array, Array)
#2 /Users/Joris/Desktop/Test envoi sms accueil/sendnotifications.php(20): Twilio\Rest\Api\V2010\Account\MessageList->create('$phone', Object(Twilio\Values))
#3 {main}
  thrown in /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Version.php on line 85
MacBook-Pro:Test envoi sms accueil Joris$

1 个答案:

答案 0 :(得分:0)

我只是重新尝试了第二个代码并且它正在工作,我不知道为什么:D

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'My id';
$token = 'My token';
$client = new Client($sid, $token);
$phone='MyPhoneNumber';

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    $phone,
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => 'MyTwilioSendingNumber',
        // the body of the text message you'd like to send
        'body' => "My message"
    )
);