Search code examples
phpamazon-sqs

SQS SendMessage & $_POST Issue


If I put $author='Steven King'; it works without issue, however it does not work with a post variable.

To be "clear" if I hard code the the author in the JSON it will in fact post the message to the SQS queue. This is the expected result, however if I pass the string from the Post Variable e.g. $author=$_POST['author], the message is never delivered.

$message = array(
                    // Associative array of custom 'String' key names
                    'Author' => array(
                        'StringValue' =>$author,
                        'DataType' => 'String'
            ),
        );

Any thoughts or help on this I would be grateful.

<?php
    $author =$_POST["author"];
    
    require 'vendor/autoload.php';
    use Aws\Common\Aws;
    use Aws\Sqs\SqsClient;
    use Aws\Exception\AwsException;
    
    
    // Get the client from the builder by namespace
    
    $client = SqsClient::factory(array(
        'profile' => 'default',
        'region'  => 'us-west-2',
        'version' => '2012-11-05'
    ));
    
    $queueUrl ='https://sqs.us-west-2.amazonaws.com/blahblahblah';
    
    
    $message = array(
                    // Associative array of custom 'String' key names
                    'Author' => array(
                        'StringValue' =>$author,
                        'DataType' => 'String'
            ),
        );
    
    var_dump($message);
    $result = $client->sendMessage(array(
    'QueueUrl' => $queueUrl,
    'MessageBody' => 'An awesome message!',
    'MessageAttributes' =>$message,
    ));

Solution

  • So the issue was caused by the credential provider, which is why it worked in the cli e.g. php posts.php and not in the browser. Because in the CLI it has the correct environment and permissions.

    Note: However on AWS SDK example does not include the credential provider only a reference to 'profile' => 'default' which would be thought to grab your default credentials, however that is not the case.

    Thank you apache logs!

    The fix was to set the right permissions on the /.aws/credentials and ensure that your $HOME path is set correctly.

    On to the next piece. Thanks community.