Cannot get aws lambda function response in PHP client

一曲冷凌霜 提交于 2019-12-21 04:41:02

问题


I am trying to call a simple Aws Lambda function using PHP as Instructed in the documentation, But I am not getting the desired response.

PHP Lambda client

require './aws/aws-autoloader.php';
use Aws\Lambda\LambdaClient;

$client = LambdaClient::factory(array(
            'version' => "latest",
            'credentials' => array(
                'key' => '*******',
                'secret' => '*******'
            ),
            'region' => '*******'
        ));

$response = $client->invoke([
    'FunctionName' => 'myLambda', // REQUIRED
    'InvocationType' => 'RequestResponse',
    'Payload' => '{"key":"value"}',
        ]);

echo "<pre>";
print_r($response);
print_r($response->data);

?>

Node.js Lambda function This has nothing but this simple code that returns "success" on successful execution of the Lambda function. Its working find in the Amazon Lambda console.

exports.handler = function(event, context){

    context.succeed("success");
};

Response from Amazon I am getting a Private data object, that I cannot access. And according to the documentation, Payload is supposed to be the response from the function. But, I am getting an Object, which again I cannot access, because the parent object data is private.

Aws\Result Object
(
    [data:Aws\Result:private] => Array
        (
            [Payload] => GuzzleHttp\Psr7\Stream Object
                (
                    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #6
                    [size:GuzzleHttp\Psr7\Stream:private] => 
                    [seekable:GuzzleHttp\Psr7\Stream:private] => 1
                    [readable:GuzzleHttp\Psr7\Stream:private] => 1
                    [writable:GuzzleHttp\Psr7\Stream:private] => 1
                    [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
                    [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                        (
                        )

                )

            [StatusCode] => 200
            [FunctionError] => 
            [LogResult] => 
            [@metadata] => Array
                (
                    [statusCode] => 200
                    [effectiveUri] => https://lambda.*********.amazonaws.com/2015-03-31/functions/myLambda/invocations
                    [headers] => Array
                        (
                            [content-type] => application/json
                            [date] => Wed, 06 Apr 2016 12:33:05 GMT
                            [x-amzn-remapped-content-length] => 0
                            [x-amzn-requestid] => ******-*****-*****-****-*******************
                            [content-length] => 9
                            [connection] => keep-alive
                        )

                    [transferStats] => Array
                        (
                            [http] => Array
                                (
                                    [0] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

So, how do I access the Response from the Lambda function? What might be the issue here?

UPDATE

I am able to access the Payload by using print_r($response['Payload']); But, Still its useless because the Payload is not coming as expected.


回答1:


Oh! well, I found the answer. You need to call the __toString() method of the GuzzleHttp\Psr7\Stream Object that is inside the Payload.

So, doing a print_r($response['Payload']->__toString()); prints "Success" which is the desired response of the Lambda function, and the one that I was looking for.

Hope this helps someone in the future.




回答2:


Another way is to call, getContents() of the stream object as following:

$result = $client->invoke(array(
          // FunctionName is required
          'FunctionName' => 'myService-beta-hello',
          'InvocationType' => 'RequestResponse',
            'LogType' => 'Tail',
            'Payload' => '{"key1":"value1", "key2":"value2","key3":"value3"}',
            //'Qualifier' => 'string',
                ));
print "<pre>";
print_r($result);
print_r($result['Payload']->getContents());
print "</pre>";



回答3:


require_once 'aws/aws-autoloader.php';
use Aws\Lambda\LambdaClient;
$client = LambdaClient::factory([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => [
        'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
     ]
]);

$result = $client->invoke([
    // The name your created Lamda function
    'FunctionName' => 'calculation',
]);

var_dump((string)$result->get('Payload'));


来源:https://stackoverflow.com/questions/36451925/cannot-get-aws-lambda-function-response-in-php-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!