PHP,Twilio SMS-捕获twilio的响应

问题描述 投票:1回答:1

我正在尝试使用Twilio发送一些SMS消息,到目前为止,这很简单,但是我正在使用一些测试数据来捕获各种情况,并且我使用“ 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;
   }

我如何捕获该回复?

php twilio-php
1个回答
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();
      }
© www.soinside.com 2019 - 2024. All rights reserved.