You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中处理Amazon SES响应并提取模板名称

如何从AWS SES的ListTemplates结果中提取模板名称?

问题背景

我用以下代码获取AWS 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) {
    // 失败时输出错误信息
    echo $e->getMessage();
    echo "\n";
}

var_dump输出的$result结构如下:

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) {
  }
}

我尝试了以下两种循环方式都无法正常运行,想知道正确提取所有模板Name字段的方法:

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

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

解决方法

$result是Aws\Result类的实例,它的内部数据存储在私有属性中,不能直接通过foreach遍历对象本身。必须通过AWS SDK提供的方法来访问内部数据,以下是两种可行的方案:

方案一:使用get()方法直接获取目标数组

try {
    $result = $SesClient->listTemplates([
        'MaxItems' => 10,
    ]);
    // 获取TemplatesMetadata数组
    $templatesMetadata = $result->get('TemplatesMetadata');
    // 循环提取每个模板的Name
    foreach ($templatesMetadata as $template) {
        echo $template['Name'] . "<br><br>";
    }
} catch (AwsException $e) {
    echo $e->getMessage();
    echo "\n";
}

方案二:将Result对象转为关联数组后处理

try {
    $result = $SesClient->listTemplates([
        'MaxItems' => 10,
    ]);
    // 把Result对象转为关联数组
    $resultArray = $result->toArray();
    // 循环TemplatesMetadata数组提取Name
    foreach ($resultArray['TemplatesMetadata'] as $template) {
        echo $template['Name'] . "<br><br>";
    }
} catch (AwsException $e) {
    echo $e->getMessage();
    echo "\n";
}

为什么之前的循环失败?

  • 第一种循环:直接遍历$result对象,实际上是在尝试访问对象的私有属性,PHP不允许直接访问私有属性,因此会报错。
  • 第二种循环:错误地将数组元素当作对象来访问($value->Name),但TemplatesMetadata里的每个元素都是关联数组,应该用$value['Name']来访问,且同样因为直接遍历对象导致无法获取正确数据。

内容的提问来源于stack exchange,提问作者Clebson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:01:48