0

I'm trying to read messages from my SQS but I can't get that far as I'm having trouble installing the PHP SDK.

I followed the instructions and installed via composer.

My code for testing purposes is:

require 'vendor/autoload.php';
$sqs_credentials = array(
        'region' => 'us-west-2',
        'version' => 'latest',
        'credentials' => array(
            'key'    => '*****',
            'secret' => '**********',
        )
    );

    $sqs_client = new SqsClient($sqs_credentials);

This simply results in

Class 'SqsClient' not found in /var/www/html/sqs_test.php on line 10

I then tried by downloading the zip file directly and used

require 'aws/aws-autoloader.php';

This resulted in the exact same error. What am I doing wrong? I'm quite certain the path is accurate as I can output text directly on Sqs/SqsClient.php.

user2029890
  • 2,493
  • 6
  • 34
  • 65

3 Answers3

1

Still not sure why the above didn't work, but if anyone is trying, this does work:

require 'vendor/autoload.php';
use Aws\Sqs\SqsClient;

$client = SqsClient::factory(array(
        'region' => 'us-west-2',
        'version' => 'latest',
        'credentials' => array(
            'key'    => '********',
            'secret' => '********',
        )
    )
);

$result = $client->receiveMessage(array(
    'QueueUrl' => $sqs_url
));
print_r($result);
user2029890
  • 2,493
  • 6
  • 34
  • 65
  • The `factory` method just calls the class's constructor with the provided array. The difference between the code in your question and the code in this answer is the presence of an import statement: `use Aws\Sqs\SqsClient;` – giaour Mar 17 '16 at 17:56
1

The class you're trying to instantiate is namspaced. Remember to include a use Aws\Sqs\SqsClient; statement before referring to the class by its short name.

giaour
  • 3,878
  • 2
  • 25
  • 27
0

Somewhat related but not exactly this issue. I had a class not found for Aws\Sns\MessageValidator (& Message) and after some struggle found out that latest SDK v3.17.3 for PHP didn't have them under \Sns - https://github.com/aws/aws-sdk-php/tree/3.17.3/src/Sns. I simply rolled back to an earlier version 3x and corrected the import path to get the class we needed. Posting here thinking it may of help to someone.