如何在JSON对象/数组中获取属性

时间:2019-06-18 19:34:20

标签: php json

我目前正在使用Amazon AWS SNS向客户发送短信。

SMS可以正常工作,但是我想根据结果将表单提交后简单地显示成功或错误消息。

表格提交成功后的结果如下。我可以看到带有int(200)的statusCode表示它已成功发送。如何获取并使用它显示成功或错误消息?

object(Aws\Result)#117(2){
  [
    "data": "Aws\Result": private
  ]=>array(2){
    [
      "MessageId"
    ]=>string(36)"f12f2261-5e13-54e8-b72e-37s26fd3c348"[
      "@metadata"
    ]=>array(4){
      [
        "statusCode"
      ]=>int(200)[
        "effectiveUri"
      ]=>string(35)"https://sns.eu-west-1.amazonaws.com"[
        "headers"
      ]=>array(4){
        [
          "x-amzn-requestid"
        ]=>string(36)"716dase5-f048-5d35-8af0-sf36ce583d95"[
          "content-type"
        ]=>string(8)"text/xml"[
          "content-length"
        ]=>string(3)"294"[
          "date"
        ]=>string(29)"Tue, 18 Jun 2019 19:31:28 GMT"
      }[
        "transferStats"
      ]=>array(1){
        [
          "http"
        ]=>array(1){
          [
            0
          ]=>array(0){

          }
        }
      }
    }
  }[
    "monitoringEvents": "Aws\Result": private
  ]=>array(0){

  }
}

php代码:

if(isset($_POST['gateeway'])){

    $sender_id = $_POST['sender_id'];
    $message = $_POST['message'];
    $topic = 'arn:aws:sns:eu-west-1:52732446504:Testing';

    try {
        $result = $sns->publish([
            'TargetArn' => $topic,
            'Message' => $message,
            'MessageAttributes' => [
                'AWS.SNS.SMS.SenderID' => [
                    'DataType' => 'String', 
                    'StringValue' => $sender_id,
                ],
                'AWS.SNS.SMS.SMSType' => [
                    'DataType' => 'String',
                    'StringValue' => 'Promotional',
                ]
            ]
        ]);
        var_dump($result);
    } catch (AwsException $e) {
        // output error message if fails
        error_log($e->getMessage());
    } 
}

1 个答案:

答案 0 :(得分:0)

您可以获得这样的状态代码

$metaInfo = $result->get('@metadata');
if($metaInfo ['statusCode'] === 200){
  echo "Message Sent";
}