LoginSignup
41
40

More than 5 years have passed since last update.

PHP から Slack の Incoming WebHooks を叩く

Last updated at Posted at 2016-05-19

ググッて出てくる情報が古かったり間違っていたり curl 使わなきゃいけなかったりして試行錯誤したのでメモ。

<?php

function send_to_slack($message) {
  $webhook_url = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX';
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-Type: application/json',
      'content' => json_encode($message),
    )
  );
  $response = file_get_contents($webhook_url, false, stream_context_create($options));
  return $response === 'ok';
}

$message = array(
  'username' => 'Bot',
  'text' => 'fooooo!!!',
);

send_to_slack($message);

素直に POST で JSON を投げつければいいんです。

41
40
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
40