简体   繁体   English

如何序列化 WhatsApp 消息有效负载以在 Laravel php 中获取消息正文、phone_number_id、联系人

[英]How can I serialize WhatsApp message payload to fetch the message body, phone_number_id, contact in Laravel php

{
  "field": "messages",
  "value": {
    "messaging_product": "whatsapp",
    "metadata": {
      "display_phone_number": "16505551111",
      "phone_number_id": "123456123"
    },
    "contacts": [
      {
        "profile": {
          "name": "test user name"
        },
        "wa_id": "16315551181"
      }
    ],
    "messages": [
      {
        "from": "16315551181",
        "id": "ABGGFlA5Fpa",
        "timestamp": "1504902988",
        "type": "text",
        "text": {
          "body": "this is a text message"
        }
      }
    ]
  }
}

The easiest way is to save the response to a variable and get the data from the array as follows最简单的方法是将响应保存到变量并从数组中获取数据,如下所示

$message = $responce['value']['message'];
$phone_number = $responce['value']['metadata']['phone_number_id'];
$contact = $responce['value']['contacts'];

You can also decode the response first and then you can easily do the following您也可以先解码响应,然后您可以轻松地执行以下操作

$res = json_decode($responce);
$message = $res->value->message;
$phone_number= $res->value->metadata->phone_number_id;
$contact = $res->value->contacts;

But if you want to convert the JSON you have to a Laravel object I recommend checking out the following answer .但是,如果您想将 JSON 转换为 Laravel 对象,我建议您查看以下答案

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

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