หลังจากบทความที่แล้ว เริ่มต้นกับการใช้งาน Line Notify คราวนี้เราลองมาใช้งาน Line Notify ด้วย PHP กันบ้างครับ โดยสามารถนำ Code ด้านล่างนี้ไปประยุกค์ใช้งานได้ครับ
<?php function send_line_notify($message, $token) { $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, "https://notify-api.line.me/api/notify"); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt( $ch, CURLOPT_POST, 1); curl_setopt( $ch, CURLOPT_POSTFIELDS, "message=$message"); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1); $headers = array( "Content-type: application/x-www-form-urlencoded", "Authorization: Bearer $token", ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec( $ch ); curl_close( $ch ); return $result; } $message = 'ข้อความ'; $token = 'ใส่ token ของคุณ'; echo send_line_notify($message, $token); ?>
หากไม่มีอะไรผิดพลาด จะได้การตอบกลับมาจากทาง Line เป็น
{"status":200,"message":"ok"}