0

I'm trying to create a function in Wordpress that sends data with JSON through Curl to a Slack webhook. I'm more used to the way you structure arrays and objects in JavaScript, so doing this in PHP before it's being encoded to JSON is a bit confusing to me.

This is how the PHP for this encoding looks:

$data = json_encode(array(
 "username"=> "Email Notifier",
 "mrkdwn"=> true,
 "icon_emoji"=> ":email:",
 "text" => "*Name:* {$posted_data["your-name"]}\n*Email:* {$posted_data["your-email"]}\n*Subject:* {$posted_data["your-subject"]}\n*Message:*\n >{$posted_data["your-message"]}",
 ));
 // Finally send the data to Zapier or your other webhook endpoint
 $ch = curl_init("https://hooks.slack.com/services/Txxxxxx/Bxxxxxx/xxxxxxxxxxxxxxx"); // replace with your Zapier webook
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); //Optional timeout value
 curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Optional timeout value
 $result = curl_exec($ch);
 curl_close($ch);
 return $result;

This sends the following JSON to the Slack webhook successfully:

{
 "username": "Email Notifier",
 "mrkdwn": true,
 "icon_emoji": ":email:",
 "text": "whatever" //generated by using PHP variables
}

I now want to be able to output the text property and additional properties within an array called attachment, like this:

{
 "username": "Email Notifier",
 "mrkdwn": true,
 "icon_emoji": ":email:",
 "text": "You have received a new message!",
 "attachment": [
 { 
 "text": "whatever", //generated by using PHP variables
 "fallback": "Required plain-text summary of the attachment.",
 "color": "#36a64f",
 }
 ]
}

But I'm not really sure how to solve this with the PHP syntax. Any ideas?

asked Jan 10, 2018 at 15:04
4
  • Be mindful of your double-quotes in double-quotes ... as that causes concatenation issues. You'll need to use single quotes on like $posted_data['your-name'] instead of doubles. Commented Jan 10, 2018 at 15:07
  • 3
    array('attachment' => array(array('text' => 'whatever', ...))) Commented Jan 10, 2018 at 15:07
  • Read about PHP arrays. They are not difficult. Basically, PHP arrays work similar to Javascript objects handled using the array notation. json_encode() handles the proper encoding as JSON. Commented Jan 10, 2018 at 15:12
  • If you have a sample of desired json, say print_r(json_decode($sample)); and see what array structure you need Commented Jan 10, 2018 at 16:25

1 Answer 1

1

Try this, like @deceze says.

$data = json_encode(array(
 "username"=> "Email Notifier",
 "mrkdwn"=> true,
 "icon_emoji"=> ":email:",
 "text" => "*Name:* {$posted_data["your-name"]}\n*Email:* {$posted_data["your-email"]}\n*Subject:* {$posted_data["your-subject"]}\n*Message:*\n >{$posted_data["your-message"]}",
 "attachment" => array(
 array( 
 "text" => "whatever",
 "fallback" => "Required plain-text summary of the attachment.",
 "color" => "#36a64f",
 ),
 ),
));
// Finally send the data to Zapier or your other webhook endpoint
 $ch = curl_init("https://hooks.slack.com/services/Txxxxxx/Bxxxxxx/xxxxxxxxxxxxxxx"); // replace with your Zapier webook
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); //Optional timeout value
 curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Optional timeout value
 $result = curl_exec($ch);
 curl_close($ch);
return $result;

Also take a look of this section on the PHP official documentation json_encode()

tobiasg
1,0735 gold badges21 silver badges39 bronze badges
answered Jan 10, 2018 at 15:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.