0

How can I use php string inside this Json array ? I want to replace the name "john" according php string. but whatever I do it is getting treated as plane text.

I tried "text": "'.$fullname.'" and even "text": ""

It is used code for 360dialog Whatsapp API.

$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": "john"
                        },
                        {
                            "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;

https://docs.360dialog.com/docs/whatsapp-api/whatsapp-api/sandbox#php

  • 2
    Instead of manually write the JSON like that, create a PHP array with the structure you want and use `json_encode()` to encode it into json. Not only does it remove the risk of typos that could cause invalid JSON, but you can then just add the variable as the value for the element you want, just like you would for any other PHP variable. – M. Eriksson Nov 16 '22 at 10:44
  • You can use `<<<"PAYLOAD"` or just `<< – Foobar Nov 16 '22 at 10:46

1 Answers1

0

For using variables inside of a heredoc you can use <<<"PAYLOAD" or <<<PAYLOAD.

Only <<<'PAYLOAD' syntax does not parse the variables inside of the heredoc and take them as just plain text.

https://www.php.net/manual/de/language.types.string.php#language.types.string.syntax.heredoc

Foobar
  • 769
  • 1
  • 4