Skip to content

Instantly share code, notes, and snippets.

@pascalwacker
Created September 27, 2016 17:50
Show Gist options
  • Save pascalwacker/4a85a04e844fa72f59626eb3fea5c0f7 to your computer and use it in GitHub Desktop.
Save pascalwacker/4a85a04e844fa72f59626eb3fea5c0f7 to your computer and use it in GitHub Desktop.
Mattermost PHP Post example
// target webhook
$target = 'https://dev.bermos.tech/hooks/xxx'; // get your webhook at "Integrationen" at the settings
// message body
$body = "| Course | Time | Room | Video | Prof |\n";
$body .= "|:--|:--|:--|\n";
$body .= "| Test | XX:XX-YY:YY | HG E7 | :white_check_mark: HG E5 | FooBar |\n";
$body .= "| Test 2 | XX:XX-YY:YY | HG E7 | No | FooBar |\n";
$body .= "\n";
// this is an example for a course bot, using a table. Mattermost supports MD
// https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
// payload construction
$payload = 'payload={"text": "';
$payload .= str_replace('"', '\"', $body);
$payload .= '"}';
// init cURL
$ch = curl_init();
// set cURL options
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// enable return and execute
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
// close connection
curl_close($ch);
// check if the request was processed
if ($server_output == 'ok') {
var_dump('yayy every thing works!');
} else {
var_dump('shit! something is broken...');
}
// output return of the server
var_dump($server_output);
// output target url and sen't payload
var_dump($target, $payload);
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment