PHP“使用”在函数中使用时导致语法错误

时间:2018-08-15 04:25:45

标签: php twilio

我对此感到困惑,而且绝对不会遇到这个问题。我正在尝试设置一个测试脚本以使用twilio(短信服务)。我有一个脚本sms.php,其后是

require ('twilo/twilio-php-master/Twilio/autoload.php');
use Twilio\Rest\Client;

$message = "testing this out to see if it works";

// Your Account SID and Auth Token from twilio.com/console
$account_sid = '*****************************';
$auth_token = '*****************************';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]

// A Twilio number you own with SMS capabilities
$twilio_number = "+11234567890";

$client = new Client($account_sid, $auth_token);
$client->messages->create(
    // Where to send a text message (your cell phone?)
    '+11234567890',
    array(
        'from' => $twilio_number,
        'body' => $message
    )
);

上面的脚本可以正常工作,没有错误。出于某种原因,如果我将其放在函数中,则会在第二行use中导致语法错误,并且脚本将无法正常工作,为什么会发生这种情况?

function sms() {
    require ('twilo/twilio-php-master/Twilio/autoload.php');
    use Twilio\Rest\Client; <-- this line errors

    $message = "testing this out to see if it works";

    // Your Account SID and Auth Token from twilio.com/console
    $account_sid = '*****************************';
    $auth_token = '*****************************';
    // In production, these should be environment variables. E.g.:
    // $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]

    // A Twilio number you own with SMS capabilities
    $twilio_number = "+11234567890";

    $client = new Client($account_sid, $auth_token);
    $client->messages->create(
        // Where to send a text message (your cell phone?)
        '+11234567890',
        array(
            'from' => $twilio_number,
            'body' => $message
        )
    );
}

enter image description here

0 个答案:

没有答案