Home > Back-end >  How to handle Amazon SES response in PHP?
How to handle Amazon SES response in PHP?

Time:11-28

I use the following to get the list of templates created in SES:

$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

try {
    $result = $SesClient->listTemplates([
        'MaxItems' => 10,
    ]);
    echo '<pre>';
    var_dump($result );
    echo '</pre>';
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}

The output for $result, from var_dump is as follows:

object(Aws\Result)#165 (2) {
  ["data":"Aws\Result":private]=>
  array(2) {
    ["TemplatesMetadata"]=>
    array(2) {
      [0]=>
      array(2) {
        ["Name"]=>
        string(10) "MyTemplate"
        ["CreatedTimestamp"]=>
        object(Aws\Api\DateTimeResult)#179 (3) {
          ["date"]=>
          string(26) "2022-11-27 03:44:26.567000"
          ["timezone_type"]=>
          int(2)
          ["timezone"]=>
          string(1) "Z"
        }
      }
      [1]=>
      array(2) {
        ["Name"]=>
        string(17) "confirmarCadastro"
        ["CreatedTimestamp"]=>
        object(Aws\Api\DateTimeResult)#180 (3) {
          ["date"]=>
          string(26) "2022-11-05 08:10:26.979000"
          ["timezone_type"]=>
          int(2)
          ["timezone"]=>
          string(1) "Z"
        }
      }
    }
    ["@metadata"]=>
    array(4) {
      ["statusCode"]=>
      int(200)
      ["effectiveUri"]=>
      string(37) "https://email.sa-east-1.amazonaws.com"
      ["headers"]=>
      array(5) {
        ["date"]=>
        string(29) "Sun, 27 Nov 2022 23:53:59 GMT"
        ["content-type"]=>
        string(8) "text/xml"
        ["content-length"]=>
        string(3) "576"
        ["connection"]=>
        string(10) "keep-alive"
        ["x-amzn-requestid"]=>
        string(36) "26bf55b1-768e-44c1-b250-e6ff61de94df"
      }
      ["transferStats"]=>
      array(1) {
        ["http"]=>
        array(1) {
          [0]=>
          array(0) {
          }
        }
      }
    }
  }
  ["monitoringEvents":"Aws\Result":private]=>
  array(0) {
  }
}

How do I handle this data in php and generate the loop correctly?

I tried the following:

foreach ($result as $key=>$value) {
    echo $value[$key]['Name'], "\n<br><br>";
}

OR

foreach ($result as $value) {
    echo $value->Name, "\n<br><br>";
}

The code does not work at all, what is the correct way?

I need to retrieve only the values of Name, the others are unnecessary.

CodePudding user response:

As per the result syntax given here you need to do it like this-

foreach ($result["TemplatesMetadata"] as $key=>$value) {
    echo $key, $value['Name'], "\n<br><br>";
}

OR

foreach ($result["TemplatesMetadata"] as $value) {
    echo $value['Name'], "\n<br><br>";
}
  • Related