Skip to content

Instantly share code, notes, and snippets.

@jhubert
Created April 15, 2015 03:01
Show Gist options
  • Save jhubert/15d730f635fa3a4d853a to your computer and use it in GitHub Desktop.
Save jhubert/15d730f635fa3a4d853a to your computer and use it in GitHub Desktop.
A very rough PHP script for posting form data via slack
<?php
$slack_domain = '';
$slack_token = '';
if ( isset($_POST['email']) && isset($_POST['name']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
$message = "Someone wants a demo!\n\n" .
"Name: " . $_POST['name'] . "\n" .
"Company: " . $_POST['company'] . "\n" .
"Email: " . $_POST['email'] . "\n" .
"Phone Number: " . $_POST['phone-number'] . "\n";
// Array of data posted Slack
$fields = array(
'username' => "demo_request",
'icon_emoji' => ":heart_eyes:",
'text' => $message
);
// 'payload' parameter is required by Slack
// $fields array must be json encoded
$payload = "payload=" . json_encode($fields);
// URL we need to post data to (Given to you by Slack when creating an Incoming Webhook integration)
$url = "https://$slack_domain.slack.com/services/hooks/incoming-webhook?token=$slack_token";
// Start CURL connection
$ch = curl_init();
// Set the:
// - URL
// - Number of POST variables
// - Data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $payload);
// Execute post to Slack integration
$result = curl_exec($ch);
// Close CURL connection
curl_close($ch);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment