Skip to content

Instantly share code, notes, and snippets.

@boychawin
Last active August 23, 2022 21:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boychawin/62c4eb3e0b8e585eba9f69a226ce0cc0 to your computer and use it in GitHub Desktop.
Save boychawin/62c4eb3e0b8e585eba9f69a226ce0cc0 to your computer and use it in GitHub Desktop.
แจ้งเตือนด้วย Line Notify ด้วย php
<?php
//post ข้อมูลมาเก็บไว้ที่ตัวแปร
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$number = $_POST['number'];
$company = $_POST['company'];
$messages = $_POST['message'];
///ส่วนที่ 1 line แจ้งเตือน จัดเรียงข้อความที่จะส่งเข้า line ไว้ในตัวแปร $message
$header = 'ส่งข้อความถึงเรา';
$message =
$header .
"\n" .
'ชื่อเต็ม: ' .
$fullname .
"\n" .
'อีเมล: ' .
$email .
"\n" .
'เบอร์: ' .
$number .
"\n" .
'บริษัท: ' .
$company .
"\n" .
'ข้อความ: ' .
$messages .
"\n";
///ส่วนที่ 2 line แจ้งเตือน ส่วนนี้จะทำการเรียกใช้ function sendlinemesg() เพื่อทำการส่งข้อมูลไปที่ line
sendlinemesg();
header('Content-Type: text/html; charset=utf8');
$res = notify_message($message);
///ส่วนที่ 3 line แจ้งเตือน
function sendlinemesg()
{
define('LINE_API', "https://notify-api.line.me/api/notify");
define('LINE_TOKEN', "er49yWnbO3E6PAV0lYLJtMZWClN23lsjiKHrShnc5Jv"); //เปลี่ยนใส่ Token ของเราที่นี่
function notify_message($message)
{
$queryData = array('message' => $message);
$queryData = http_build_query($queryData, '', '&');
$headerOptions = array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n"
. "Authorization: Bearer " . LINE_TOKEN . "\r\n"
. "Content-Length: " . strlen($queryData) . "\r\n",
'content' => $queryData
)
);
$context = stream_context_create($headerOptions);
$result = file_get_contents(LINE_API, FALSE, $context);
$res = json_decode($result);
return $res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment