简体   繁体   English

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

[英]Cannot get aws lambda function response in PHP client

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 调用一个简单的 Aws Lambda 函数,但我没有得到所需的响应。

PHP Lambda client 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 function This has nothing but this simple code that returns "success" on successful execution of the Lambda function. Node.js Lambda 函数除了在成功执行 Lambda 函数时返回“成功”的简单代码之外什么都没有。 Its working find in the Amazon Lambda console.它可以在 Amazon Lambda 控制台中找到。

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.根据文档,Payload 应该是函数的响应。 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?那么,如何从 Lambda 函数访问响应? What might be the issue here?这里可能有什么问题?

UPDATE更新

I am able to access the Payload by using print_r($response['Payload']);我可以使用print_r($response['Payload']);访问 Payload print_r($response['Payload']); But, Still its useless because the Payload is not coming as expected.但是,仍然没有用,因为有效载荷没有按预期到来。

Oh!哦! well, I found the answer.好吧,我找到了答案。 You need to call the __toString() method of the GuzzleHttp\\Psr7\\Stream Object that is inside the Payload .您需要调用Payload内的GuzzleHttp\\Psr7\\Stream对象的__toString()方法。

So, doing a print_r($response['Payload']->__toString());所以,做一个print_r($response['Payload']->__toString()); prints "Success" which is the desired response of the Lambda function, and the one that I was looking for.打印“成功”,这是 Lambda 函数所需的响应,也是我正在寻找的响应。

Hope this helps someone in the future.希望这对未来的人有所帮助。

Another way is to call, getContents() of the stream object as following:另一种方法是调用流对象的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>";
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'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM