1
0

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 1 year has passed since last update.

PHPでLINE通知を行う

Posted at

こんにちは。

サーバが死んでしまった場合などにLINEで通知を送る方法について調べました。
LINEのグループにも送ることができるため、複数人での開発などに利用することができます。

なお死活監視であれば専用のサービスを使った方が通知方法なども含め便利です。

下準備

LINE側で通知を送るグループ(トークルーム)を作成しておきます。

LINE Notifyでアカウントを作成します。

image.png

送信先のトークルームを選択し、トークンを発行します。

image.png

発行されたトークンを控えておきます。

Screenshot 2023-06-14 at 14.51.29.png

シェルから送信のテストを行っておきます。


下記のようなコードで通知を送ります。

<?php
define('LINE_NOTIFY_TOKEN', 'xxxxxx');


/**
 * LINEへの通知。
 *
 * @param $message
 *
 * @return void
 */
function lineNotify( $message = '' ) {
	$url = "https://notify-api.line.me/api/notify";
	$ch  = curl_init(); // はじめ
	curl_setopt( $ch, CURLOPT_URL, $url );
	curl_setopt( $ch, CURLOPT_VERBOSE, 0 ); // 詳細を表示しない
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );//変数に保存する
	curl_setopt( $ch, CURLOPT_HEADER, true ); // ヘッダも出力したい場合

	curl_setopt( $ch, CURLOPT_HTTPHEADER,
		array( 'Authorization: Bearer ' . LINE_NOTIFY_TOKEN ) );

	$post_data = array( 'message' => $message );

	curl_setopt( $ch, CURLOPT_POST, true );
	curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $post_data ) );

	$res = curl_exec( $ch ); //実行
	curl_close( $ch ); //終了
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?