永続的な接続で行われる並列リクエストのこの問題を解決しようとして AWS と SES につまずく人のために、AWS SDK 2 以降では、コマンド オブジェクトを使用して php でこれをサポートしています。
SesClient と他のクライアントは、コマンドを並行して実行できます。これは、SES を介して単一の接続と電子メールを起動する通常の方法です。
$result = $client->sendEmail(array(
//email data
));
getCommand()
クライアント オブジェクトは非常に強力で、やなどのリクエストを実行および操作するための多くのメソッドを継承していますexecute()
。簡単な解決策を見つけるまでに、何時間も掘り下げました。検索するのに適切なものを知っていればよいだけです。次に例を示します。
$commands = array();
$commands[] = $sesClient->getCommand('SendEmail', array(
//email data
));
$commands[] = $sesClient->getCommand('SendEmail', array(
//email data
));
// Execute an array of command objects to do them in parallel
$sesClient->execute($commands);
// Loop over the commands, which have now all been executed
foreach ($commands as $command) {
$result = $command->getResult();
// Do something with result
}
エラー処理は、次のコードを実行することで実現できます。
use Guzzle\Service\Exception\CommandTransferException;
try {
$succeeded = $client->execute($commands);
} catch (CommandTransferException $e) {
$succeeded = $e->getSuccessfulCommands();
echo "Failed Commands:\n";
foreach ($e->getFailedCommands() as $failedCommand) {
echo $e->getExceptionForFailedCommand($failedCommand)->getMessage() . "\n";
}
}
Amazon は、これらの例を開発者ガイドのコマンド機能の下に記載しています。