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
  • 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. Commented Nov 16, 2022 at 10:44
  • You can use <<<"PAYLOAD" or just <<<PAYLOAD to make it work. Commented Nov 16, 2022 at 10:46

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.