Saswat
Saswat

Reputation: 12846

How to check whether SMS has been successfully sent from Amazon AWS SNS service using Laravel 5.6 and PHP?

I am using the Amazon Web Server SNS to send sms to mobile numbers. Here is my code:-

    $params = array(
        'credentials'   => array(
                                'key'    => self::$smsKey,
                                'secret' => self::$smsSecret
                            ),
        'region'        => 'ap-northeast-1', // < your aws from SNS Topic region
        'version'       => 'latest'
   );
   $sns = new \Aws\Sns\SnsClient($params);

   $args = array(
       "SenderID"       => self::$senderID,
       "SMSType"        => self::$smsType,
       "Message"        => $message,
       "PhoneNumber"    => $phonenumber
   );

   $result = $sns->publish($args);
   echo "<pre>";
   var_dump($result);
   echo "</pre>";

When I var_dump the $result, I get the following data:-

object(Aws\Result)#352 (1) {
  ["data":"Aws\Result":private]=>
  array(2) {
    ["MessageId"]=>
    string(36) "xxxxxxxxxx-xxxxxa-xxxx-xxxx7-xxxxxxxxxx"
    ["@metadata"]=>
    array(4) {
      ["statusCode"]=>
      int(200)
      ["effectiveUri"]=>
      string(40) "https://xxxxxxxxxxxxxxxxxxxx.amazonaws.com"
      ["headers"]=>
      array(4) {
        ["x-amzn-requestid"]=>
        string(36) "yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyy"
        ["content-type"]=>
        string(8) "text/xml"
        ["content-length"]=>
        string(3) "294"
        ["date"]=>
        string(29) "Tue, 05 Jun 2018 10:46:11 GMT"
      }
      ["transferStats"]=>
      array(1) {
        ["http"]=>
        array(1) {
          [0]=>
          array(0) {
          }
        }
      }
    }
  }
}

How can I check if the sms has been successfully sent? There is a status code. But I can't traverse the object properly to retrieve its value.

Upvotes: 1

Views: 1509

Answers (2)

Sagar Thombare
Sagar Thombare

Reputation: 11

if ($result['@metadata']['statusCode'] === 200) {
    echo "Message sent";
}

Upvotes: 1

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

You can get the data like following.

$meta = $response_object->get('@metadata');
if($meta['statusCode'] === 200){
  echo "Message Sent";
}

Upvotes: 3

Related Questions