Installation guide

// 2. Prepare
$command->prepare();
$request = $command->getRequest();
// Note: `prepare()` also returns the request object
// 3. Execute
$command->execute();
$response = $command->getResponse();
$result = $command->getResult();
// Note: `execute()` also returns the result object
This is nice, because it gives you a chance to modify the request before it is actually sent.
$command = $client->getCommand('OperationName');
$request = $command->prepare();
$request->addHeader('foo', 'bar');
$result = $command->execute();
You don't have to manage each aspect of the state though, calling execute() will also prepare the command, and
calling getResult() will prepare and execute the command.
Using requests and responses
Request and response objects contain data about the actual requests and responses to the service.
$command = $client->getCommand('OperationName');
$command->execute();
// Get and use the request object
$request = $command->getRequest();
$contentLength = $request->getHeader('Content-Length');
$url = $request->getUrl();
// Get and use the response object
$response = $command->getResponse();
$success = $response->isSuccessful();
$status = $response->getStatusCode();
You can also take advantage of the __toString behavior of the request and response objects. If you print them
(e.g., echo $request;), you can see the raw request and response data that was sent over the wire.
To learn more, read the API docs for the Request and Response classes.
Executing commands in parallel
The AWS SDK for PHP allows you to execute multiple operations in parallel when you use command objects. This
can reduce the total time (sometimes drastically) it takes to perform a set of operations, since you can do them at
the same time instead of one after another. The following shows an example of how you could upload two files to
Amazon S3 at the same time.
$commands = array();
$commands[] = $s3Client->getCommand('PutObject', array(
'Bucket' => 'SOME_BUCKET',
'Key' => 'photos/photo01.jpg',
'Body' => fopen('/tmp/photo01.jpg', 'r'),
));
$commands[] = $s3Client->getCommand('PutObject', array(
'Bucket' => 'SOME_BUCKET',
'Key' => 'photos/photo02.jpg',
'Body' => fopen('/tmp/photo02.jpg', 'r'),
));
Command Objects
31