7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Slackへメッセージを送る(PHP)

Last updated at Posted at 2016-11-01

Slack にこんなメッセージを送ってみます

snip_20161101233028.png

ログイン後、左上のメニューから「Apps & integrations」をクリックします

snip_20161101125240.png

「Incoming WebHooks」を検索して選択します

snip_20161101125501.png

「Add Configuration」をクリックします

snip_20161101125635.png

Postするチャンネルを選択し「Add Incoming WebHooks integration」をクリックします

snip_20161101125701.png

Webhook URL が表示されるのでコピーします

snip_20161101125801.png

PHPでメッセージをPOSTします

index.php

<?php

// コピーした Webhook URL
$url = 'https://hooks.slack.com/services/xxxxx';

$message = array(
    'username' => 'なまえ',
    'icon_emoji' => ':smile:',
    'text' => 'Sending Message'
);

// メッセージをjson化します
$message_json = json_encode($message);

// payloadの値としてURLエンコードしてPOSTします
$message_post = 'payload=' . urlencode($message_json);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_post);
curl_exec($ch);
curl_close($ch);

7
9
0

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
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?