在PHP客户端无法获取aws lambda函数响应

时间:2016-04-06 12:59:36

标签: php node.js amazon-web-services aws-lambda

我试图在文档中使用PHP作为指示调用简单的Aws Lambda函数,但我没有得到所需的响应。

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控制台中找到。

exports.handler = function(event, context){

    context.succeed("success");
};

来自亚马逊的回复 我收到一个私有数据对象,我无法访问。根据文档,Payload应该是函数的响应。但是,我得到一个对象,我无法访问,因为父对象数据是私有的。

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?这可能是什么问题?

更新

我可以使用print_r($response['Payload']);访问Payload 但是,由于有效负载未达到预期效果,它仍然无用。

3 个答案:

答案 0 :(得分:11)

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

所以,做print_r($response['Payload']->__toString());打印&#34;成功&#34;这是Lambda函数的理想响应,也是我正在寻找的函数。

希望这有助于将来。

答案 1 :(得分:6)

另一种方法是调用流对象getContents(),如下所示:

$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>";

答案 2 :(得分:0)

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'));