PHP,Twilio SMS - 捕获twilio的回应

时间:2016-10-13 15:53:10

标签: php twilio-php

我正在尝试使用Twilio做一些短信,到目前为止它非常直接,但我使用一些测试数据来捕捉各种场景,我使用“1234567890”作为电话号码来捕获错误,但是当我导航到查询twilio api的页面时,我收到以下错误:

Fatal error: Uncaught exception 'Twilio\Exceptions\RestException'
with message '[HTTP 404] Unable to fetch record: 
The requested resource /PhoneNumbers/1234567890 was not found'

这是我的代码:

  use Twilio\Rest\Client;
  $client = new Client($sid, $token);

  if($ph && preg_match('/^[0-9]{10}$/', $ph)) {

        //this returns an array containing type, error_code, and a boolean       
        //value for is_valid.
        $response = lookup($client, $ph);

        if($response['is_valid']) {

            //send the message via twilio.
            $message = $client->messages->create(
                $ph,
                array(
                    'from'  =>  'my_twilio_number_goes_here',
                    'body'  =>  'text_body_goes_here'
                )
            );

            //handle twilio response
            $status = $message->status;
            $sid = $message->sid;
   }

如何捕获该响应?

1 个答案:

答案 0 :(得分:0)

      $twilio = new TwilioClient(env('TWILIO_ACCOUNT_ID'), env('TWILIO_TOKEN'));

      $message=new \StdClass;

      try
      {
        //check mobile number is valid or not
        $is_valid_number = $twilio->lookups->v1->phoneNumbers($phone_number)->fetch();
        if($is_valid_number)
        {
            $data['From']=env('TWILIO_FROM_ALPHANUMERIC_NAME');
            $data['Body']="Sms testing";
            $message = $twilio->messages->create($mobile_number, $data); 
        }
        else{
           echo "Your Mobile number is not available.";
        }
      }
      catch(\Exception $e)
      {
          echo $e->getMessage();
      }