从 twilio 请求中获取响应

Get a response from a twilio request

我正在使用 twilio 向手机发送短信,当我 运行 脚本发送短信时,它应该像我的手机一样发送短信,但问题是我不知道状态已发送的短信,如何获取?

目前我尝试过的如下

   <?php
     require __DIR__ . '/vendor/autoload.php';
     use Twilio\Rest\Client;

     // Your Account SID and Auth Token from twilio.com/console
     $account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
     $auth_token = 'your_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 = "+0987654321";

     $client = new Client($account_sid, $auth_token);
     $client->messages->create(
     // Where to send a text message (your cell phone?)
     '+1234567890',
      array(
       'from' => $twilio_number,
       'body' => 'I sent this message in under 10 minutes!'
      )
    );

在进行适当的研究并遇到各种 webhook 问题后,这就是我的决定

$phoneString = $phoneNumber->asNorthAmericanDialingString();
$mi          = $client->messages->create(
// the number you'd like to send the message to
    $phoneString ,
    array (
       // A Twilio phone number you purchased at twilio.com/console
       'from' => Config::getCurrentConfig()->smsConfig->fromNumber ,
       // the body of the text message you'd like to send
       'body' => $message , 
        'ProvideFeedback' => "true"
    )
);

$msg    = $mi->fetch();
$sid    = $msg->sid;
$status = $msg->status;
if ($status == 'sent' || $status == 'delivered') { // f'ing twilio, random status
    self::getClassLogger()->debug("Sent text message[$sid], status is [$status]");
    return new  TextMessageStatus($sid , $status);
} else {
    // do whatever is appropriate for your case
}