Skip to content

Instantly share code, notes, and snippets.

@nczz
Created March 14, 2020 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/4ebded0ffeb3ebd05e35f021b67982fc to your computer and use it in GitHub Desktop.
Save nczz/4ebded0ffeb3ebd05e35f021b67982fc to your computer and use it in GitHub Desktop.
LINE Notify 通知 PHP 範例
<?php
function mxp_line_notify($msg) {
if ($msg == "") {
return;
}
$body = array(
'message' => PHP_EOL . $msg, //先斷行,避免跟 Bot 稱呼黏在一起
);
// 授權方式
$headers = array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Bearer 把這行取代成申請的權杖',
);
$url = 'https://notify-api.line.me/api/notify';
$ch = curl_init();
$params = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($body),
);
curl_setopt_array($ch, $params);
if (!$result = curl_exec($ch)) {
if ($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
// 敵八個用
// echo "cURL error ({$errno}):\n {$error_message}";
curl_close($ch);
return FALSE;
}
} else {
curl_close($ch);
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment