无法在PHP客户端中获得AWS Lambda函数响应 [英] Cannot get aws lambda function response in PHP client

查看:153
本文介绍了无法在PHP客户端中获得AWS Lambda函数响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照文档中的说明使用PHP调用一个简单的Aws Lambda函数,但是我没有得到想要的响应.

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客户端

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函数 除了这个简单的代码,该代码在成功执行Lambda函数时返回成功",仅此而已.它可以在Amazon Lambda控制台中找到工作.

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
                                        (
                                        )

                                )

                        )

                )

        )

)

那么,如何从Lambda函数访问Response?这里可能是什么问题?

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

更新

我可以使用print_r($response['Payload']);访问有效载荷 但是,它仍然没用,因为有效负载没有达到预期的水平.

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.

推荐答案

哦!好吧,我找到了答案. 您需要调用Payload内部的GuzzleHttp\Psr7\Stream对象的__toString()方法.

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

因此,执行print_r($response['Payload']->__toString());会打印成功",这是Lambda函数以及我正在寻找的函数的期望响应.

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.

这篇关于无法在PHP客户端中获得AWS Lambda函数响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆