Last active
September 27, 2017 09:52
-
-
Save flatanimals/be85088c0dcc4cb09d7a to your computer and use it in GitHub Desktop.
[Phalcon + AWS SDK PHP] Autoload AWS and add to Dependency Injector example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| This example shows how to: | |
| - Autoload AWS SDK PHP | |
| - Add AWS SDK PHP to the Dependency Injector | |
| - Retrieve AWS dependency from DI and make a call. | |
| Note: I used composer to install my dependencies, but that is it, autoloading is handled by Phalcon | |
| composer.json: | |
| { | |
| "require": { | |
| "aws/aws-sdk-php": "dev-master" | |
| } | |
| } | |
| don't forget to: composer install --prefer-dist | |
| Trouble Shooting: | |
| If you see a PHP error like this... you forgot to configure your AWS keys | |
| PHP Fatal error: | |
| Uncaught Aws\S3\Exception\InvalidArgumentException: | |
| AWS Error Code: InvalidArgument, | |
| Status Code: 400, | |
| AWS Request ID: #AWS_REQUEST_ID#, | |
| AWS Error Type: client, | |
| AWS Error Message: Authorization header is invalid -- one and only one ' ' (space) required, | |
| User-Agent: aws-sdk-php2/2.6.7 Guzzle/3.9.1 curl/7.28.1 PHP/5.5.10 | |
| Otherwise, feel free to ask questions | |
| */ | |
| // This one goes to 11 | |
| error_reporting(E_ALL); | |
| try { | |
| // Create Configuration Object | |
| $config = new Phalcon\Config(array( | |
| 'application' => array( | |
| 'vendorDir' => __DIR__ . '/vendor/' | |
| ), | |
| // You really should store your aws key/secret values elsewhere | |
| 'aws' => array( | |
| 'key' => '*** YOUR AWS KEY GOES HERE ***', | |
| 'secret' => '** YOUR AWS SECRET GOES HERE ***', | |
| 'region' => 'us-east-1' // Change to your region | |
| ) | |
| )); | |
| // Create AutoLoader | |
| $loader = new Phalcon\Loader(); | |
| // Register Namespaces with Phalcon\Loader | |
| $loader->registerNamespaces( | |
| array( | |
| 'Aws' => $config->application->vendorDir . 'aws/aws-sdk-php/src/Aws/', | |
| 'Guzzle' => $config->application->vendorDir . 'guzzle/guzzle/src/Guzzle', | |
| 'Symfony\Component\EventDispatcher' => $config->application->vendorDir . 'symfony/event-dispatcher/Symfony/Component/EventDispatcher' | |
| ) | |
| ); | |
| $loader->register(); | |
| //Create the Dependency Injector Container | |
| $di = new Phalcon\DI\FactoryDefault(); | |
| $di['aws'] = function () use ($config) { | |
| $aws = Aws\Common\Aws::factory(array( | |
| 'key' => $config->aws->key, | |
| 'secret' => $config->aws->secret, | |
| 'region' => $config->aws->region | |
| )); | |
| return $aws; | |
| }; | |
| // Create Phalcon App | |
| $app = new \Phalcon\Mvc\Micro(); | |
| // Assign Dependency Injector | |
| $app->setDi($di); | |
| // Add Routes | |
| $app->get('/', function () use ($app, $config) { | |
| // Get AWS Dependency from Dependency Injector and instantiate S3Client | |
| $s3 = $app->getDI()->get('aws')->get('s3'); | |
| // Request a list of all buckets from AWS | |
| $result = $s3->listBuckets(); | |
| // interate and print each bucket's name on a separate line | |
| foreach ($result['Buckets'] as $bucket) echo "{$bucket['Name']}<br/>\n"; | |
| }); | |
| // Yo handle yo' business fool | |
| $app->handle(); | |
| } catch (Phalcon\Exception $e) { | |
| echo $e->getMessage(); | |
| } catch (PDOException $e) { | |
| echo $e->getMessage(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the use of Symfony EventDispatcher?