You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在360Dialog WhatsApp API的JSON Payload数组中嵌入PHP字符串

解决360Dialog WhatsApp API JSON Payload中PHP变量不解析的问题

问题原因

你当前用的是PHP的NOWDOC语法(<<<'PAYLOAD'),这种语法会把内容完全当作纯文本处理,不会解析内部的PHP变量,所以$fullname被当成字符串直接输出了。

解决方案一:改用HEREDOC语法(推荐)

去掉NOWDOC的单引号,使用无引号的HEREDOC标记,PHP会自动解析内部的变量:

$fullname = "张三"; // 示例变量
$payload = <<<PAYLOAD
{
    "to": "911212121212",
    "type": "template",
    "template": {
        "namespace": "56n9828435v89248-57498240734",
        "language": {
            "policy": "deterministic",
            "code": "en"
        },
        "name": "new_booking",
        "components": [{
                "type": "body",
                "parameters": [{
                        "type": "text",
                        "text": "$fullname"
                    },
                    {
                        "type": "text",
                        "text": "Hair Cutting"
                    },
                    {
                        "type": "text",
                        "text": "17/11/2022"
                    },
                    {
                        "type": "text",
                        "text": "04:00 PM"
                    },
                    {
                        "type": "text",
                        "text": "http://www.google.com"
                    }

                ]
            }
        ]
    }
}
PAYLOAD;

解决方案二:将JSON转为PHP数组修改后再编码(更安全)

这种方法能避免手动拼接JSON时出现语法错误,适合结构复杂的Payload:

$fullname = "张三";
// 先定义PHP数组结构
$payloadArray = [
    "to" => "911212121212",
    "type" => "template",
    "template" => [
        "namespace" => "56n9828435v89248-57498240734",
        "language" => [
            "policy" => "deterministic",
            "code" => "en"
        ],
        "name" => "new_booking",
        "components" => [
            [
                "type" => "body",
                "parameters" => [
                    ["type" => "text", "text" => $fullname],
                    ["type" => "text", "text" => "Hair Cutting"],
                    ["type" => "text", "text" => "17/11/2022"],
                    ["type" => "text", "text" => "04:00 PM"],
                    ["type" => "text", "text" => "http://www.google.com"]
                ]
            ]
        ]
    ]
];
// 转为标准JSON字符串
$payload = json_encode($payloadArray, JSON_UNESCAPED_SLASHES);

内容的提问来源于stack exchange,提问作者AbleInfosoft Gmail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 20:30:37