LoginSignup
40

More than 5 years have passed since last update.

posted at

updated at

PHP から Slack の Incoming WebHooks を叩く

ググッて出てくる情報が古かったり間違っていたり 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 を投げつければいいんです。

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
What you can do with signing up
40