PHP send slack messages with attachments and variable values
<p>Trying to <code>send messages to slack</code>channels with <code>incoming webhooks</code> installed on them. <code>Attachments</code> needs to be sent along with the message and PHP variables holds these URLs. Similary, I want to send some <code>ID's</code> which are again hold in some PHP variables. Here is my <code>server side PHP</code> code:</p>

<pre><code>&lt;?php

$testplan_name = $_POST[plan]; //test plan name coming from the client
$url1 = $_POST[run_url]; //run url coming from the client
$url2 = $_POST[plan_url]; //plan url coming from the client
$room = "random"; 
$icon_url = ":ghost:";
$username = "Test";
$attachments = array([
            'fallback' =&gt; 'Hey! See this message',
            'pretext'  =&gt; 'Here is the plan name ${testplan_name}',
            'color'    =&gt; '#ff6600',
            'fields'   =&gt; array(
                [
                    'title' =&gt; 'Run URL',
                    'value' =&gt; 'url1',
                    'short' =&gt; true
                ],
                [
                    'title' =&gt; 'Build URL',
                    'value' =&gt; 'url2',
                    'short' =&gt; true
                ]
            )
        ]);

$data = "payload=" . json_encode(array(         
        "channel"       =&gt;  "#{$room}",
        "icon_emoji"    =&gt;  $icon_url,
        "username"      =&gt;  $username,
        "attachments"   =&gt;  $attachments

    ));

$url = "https://hooks.slack.com/services/XXXX/XXX/XXXXXXXXXXXXX"; //got from slack as a webhook URL

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
echo var_dump($result);
if($result === false)
{
    echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);
</code></pre>

<p>If you see in the <code>attachments</code> variable above, there is variable inside of <code>pretext</code> which tries to print the value of <code>${testplan_name}</code> declared at the top. However, it does not seem to work and the program is failed to post messages to slack channels. Similarly, I want to print values of <code>url1</code> and <code>url2</code> in the <code>attachments -&gt; fields</code> values as can be seen above(the way I am trying to print). The program just works fine if I do not try to use any variables and get their values while posting messages. How do I print the values of these variables in messages?</p>

<p><code>(slack is a messaging platform for teams, if you don't know)</code></p>