如何在对象/数组中访问TopicArn

时间:2019-03-01 17:46:51

标签: php arrays object

我目前正在尝试在我正在执行的PHP学习项目中实施Amazon SNS。我很想知道如何在PHP中从该对象打印TopicArn,以便在以后需要在以后的代码中使用它时可以在变量中使用它。

我本以为是object(Aws\Result)#100->...,但我不确定

我尝试了$TopicArn = $sns->get('TopicArn');,但返回了

Fatal error: Uncaught TypeError: Argument 2 passed to Aws\AwsClient::getCommand() must be of the type array, string given, called in /var/www/html/vendor/aws/aws-sdk-php/src/AwsClientTrait.php on line 77 and defined in /var/www/html/vendor/aws/aws-sdk-php/src/AwsClient.php:228 Stack trace: #0 /var/www/html/vendor/aws/aws-sdk-php/src/AwsClientTrait.php(77): Aws\AwsClient->getCommand('get', 'TopicArn') #1 /var/www/html/index.php(22): Aws\AwsClient->__call('get', Array) #2 {main} thrown in /var/www/html/vendor/aws/aws-sdk-php/src/AwsClient.php on line 228

最终结果应arn:aws:sns:eu-west-1:321273dfffa23055886:testing22存储在变量中

object(Aws\Result)#100 (2) {
  ["data":"Aws\Result":private]=>
  array(2) {
    ["TopicArn"]=>
    string(44) "arn:aws:sns:eu-west-1:321273dfffa23055886:testing22"
    ["@metadata"]=>
    array(4) {
      ["statusCode"]=>
      int(200)
      ["effectiveUri"]=>
      string(35) "https://sns.eu-west-1.amazonaws.com"
      ["headers"]=>
      array(5) {
        ["x-amzn-requestid"]=>
        string(36) "b697234344422cvqe41a72-7f70-5ae6-a454dc-4e810f36483d"
        ["content-type"]=>
        string(8) "text/xml"
        ["content-length"]=>
        string(3) "316"
        ["date"]=>
        string(29) "Fri, 01 Mar 2019 17:38:14 GMT"
        ["connection"]=>
        string(5) "close"
      }
      ["transferStats"]=>
      array(1) {
        ["http"]=>
        array(1) {
          [0]=>
          array(0) {
          }
        }
      }
    }
  }
  ["monitoringEvents":"Aws\Result":private]=>
  array(0) {
  }
}

2 个答案:

答案 0 :(得分:0)

假设$obj是您在PHP中的对象变量。然后,您可以

echo $obj->get('TopicArn');

答案 1 :(得分:0)

根据AWS documentation,您可以使用get方法来做到这一点:

$yourObject->get("TopicArn");