ไลบรารี PHP สำหรับเรียก LINE Messaging API
การเรียก HTTP ทั้งหมดใช้ guzzlehttp/guzzle ^7 หรือ ^8 Composer ของแอปจะเลือกสายที่เข้ากับแพ็กเกจอื่นได้ เมื่อ LINE ตอบ 4xx/5xx Guzzle จะโยน GuzzleHttp\Exception\ClientException หรือ ServerException
ยังไม่ได้ยิง API จริงทุกเมธอดในเครื่องนี้ ให้ทดสอบกับ Channel ของคุณเองก่อนขึ้นโปรดักชัน
- PHP 8.0 ขึ้นไป
- Composer
- Channel ของ Messaging API: Channel ID, Channel secret, Channel access token
composer require deawx/linebotComposer จะดึงแพ็กเกจจาก Packagist และลง guzzlehttp/guzzle (^7 หรือ ^8) ให้เอง
ตัวอย่างโค้ดอยู่ที่โฟลเดอร์ examples/ บน GitHub (ไม่ติดตอน composer require)
ไลบรารีไม่ได้ติดตั้ง vlucas/phpdotenv เพราะการอ่านไฟล์ .env เป็นหน้าที่ของแอป
getenv() เป็นฟังก์ชันของ PHP ใช้ได้เลยถ้าเซ็ต environment ที่เว็บเซิร์ฟเวอร์ / Docker / ระบบปฏิบัติการแล้ว
ถ้าต้องการไฟล์ .env ให้ติดตั้ง dotenv ในแอป:
composer require vlucas/phpdotenvrequire __DIR__ . '/vendor/autoload.php';
use Dotenv\Dotenv;
use Cyberthai\Linebot\Line;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$line = new Line([
'channelId' => $_ENV['LINE_CHANNEL_ID'],
'clientSecret' => $_ENV['LINE_CHANNEL_SECRET'],
'channelAccessToken' => $_ENV['LINE_CHANNEL_ACCESS_TOKEN'],
]);ตัวอย่าง .env (อย่า commit ไฟล์นี้):
LINE_CHANNEL_ID=
LINE_CHANNEL_SECRET=
LINE_CHANNEL_ACCESS_TOKEN=ใส่ค่าตรงๆ ก็ได้ ใช้ clientId แทน channelId ได้เช่นกัน ถ้าค่าใดว่างตอนสร้าง จะโยน Cyberthai\Linebot\Exceptions\LineInitializeException
require __DIR__ . '/vendor/autoload.php';
use Cyberthai\Linebot\Line;
$line = new Line([
'channelId' => getenv('LINE_CHANNEL_ID'),
'clientSecret' => getenv('LINE_CHANNEL_SECRET'),
'channelAccessToken' => getenv('LINE_CHANNEL_ACCESS_TOKEN'),
]);ในตัวอย่างด้านล่างใช้ข้อความแบบนี้:
$text = [['type' => 'text', 'text' => 'สวัสดี']];| หมวด | จุดเข้า |
|---|---|
| บัญชีทางการ | $line->info() |
| Webhook | $line->webhook() |
| เนื้อหาจากผู้ใช้ | $line->content() |
| ส่งข้อความ | $line->message() |
| ผู้ใช้งาน | $line->users() |
| กลุ่ม | $line->group() |
| ห้องแชทหลายคน | $line->room() |
| ริชเมนู | $line->richmenu() |
| Audience | $line->audience() |
| สถิติ Insight | $line->insight() |
| แผนสมาชิก | $line->membership() |
| คูปอง | $line->coupon() |
| ผูกบัญชี | $line->accountLink() |
| Channel Access Token | $line->oauth() / $line->oauth2() |
$line->info()
ดึงข้อมูลบัญชีทางการ เช่น ชื่อ รูป และโหมดแชท
$line->info();$line->webhook()
ตั้งค่า ดู และทดสอบ webhook ของช่อง
เมธอดในหมวดนี้:
$line->webhook();
$line->webhook()->set($url);
$line->webhook()->get();
$line->webhook()->test();
$line->webhook()->test($url);ตั้ง URL webhook ต้องเป็น HTTPS
$line->webhook()->set('https://example.com/webhook');ดู URL และสถานะที่ตั้งไว้
$line->webhook()->get();ทดสอบว่า LINE ยิง webhook ถึงเซิร์ฟเวอร์ได้หรือไม่ ไม่ส่ง URL จะทดสอบค่าที่ตั้งไว้แล้ว
$line->webhook()->test();
$line->webhook()->test('https://example.com/webhook');$line->content()
ดาวน์โหลดไฟล์ที่ผู้ใช้ส่งมา ยิงไป https://api-data.line.me
เมธอดในหมวดนี้:
$line->content();
$line->content()->get($messageId);
$line->content()->transcoding($messageId);
$line->content()->preview($messageId);ดาวน์โหลดรูป วิดีโอ หรือเสียง คืนอาร์เรย์ที่มี content (ไบนารี) และ contentType
$file = $line->content()->get($messageId);
file_put_contents('photo.jpg', $file['content']);ตรวจว่าวิดีโอหรือเสียงพร้อมดาวน์โหลดหรือยัง
$line->content()->transcoding($messageId);ดาวน์โหลดรูปตัวอย่างของรูปหรือวิดีโอ
$line->content()->preview($messageId);$line->message()
ส่งข้อความ ดูโควตา ตรวจโครงสร้างข้อความ และจัดการแชท
เมธอดในหมวดนี้:
$line->message();
$line->message()->reply($replyToken, $text);
$line->message()->push($userId, $text);
$line->message()->multicast([$userId1, $userId2], $text);
$line->message()->narrowcast($text, $recipient);
$line->message()->progressNarrowcast($requestId);
$line->message()->broadcast($text);
$line->message()->loading($userId, 5);
$line->message()->markAsRead($markAsReadToken);
$line->message()->quota();
$line->message()->quotaConsumption();
$line->message()->deliveryReply('20260919');
$line->message()->deliveryPush('20260919');
$line->message()->deliveryMulticast('20260919');
$line->message()->deliveryBroadcast('20260919');
$line->message()->validateReply($text);
$line->message()->validatePush($text);
$line->message()->validateMulticast($text);
$line->message()->validateNarrowcast($text);
$line->message()->validateBroadcast($text);
$line->message()->aggregationInfo();
$line->message()->aggregationList();ตอบกลับด้วย replyToken จาก webhook ใช้ได้ครั้งเดียวภายในเวลาที่ LINE กำหนด
$line->message()->reply($replyToken, $text);
$line->message()->reply($replyToken, $text, true);ส่งหา user / group / room ได้ทุกเมื่อ $retryKey เป็น UUID สำหรับส่งซ้ำ
$line->message()->push($userId, $text);
$line->message()->push($userId, $text, false, $uuid);ส่งข้อความชุดเดียวกันหาหลาย user ID (ส่งกลุ่มหรือห้องไม่ได้)
$line->message()->multicast([$userId1, $userId2], $text);$line->message()->narrowcast($messages, $recipient = [], $demographic = [], $limit = 0, $notificationDisabled = false, $retryKey = '')
ส่งตาม audience หรือตัวกรองประชากร
$line->message()->narrowcast($text, [
'type' => 'audience',
'audienceGroupId' => 1234567890123,
]);ดูสถานะการส่ง narrowcast ใช้ requestId จาก header X-Line-Request-Id
$line->message()->progressNarrowcast($requestId);ส่งหาเพื่อนทั้งหมดของบัญชีทางการ
$line->message()->broadcast($text);แสดงแอนิเมชันกำลังพิมพ์ในแชท 1 ต่อ 1 วินาทีต้องเป็นพหุคูณของ 5 ช่วง 5–60
$line->message()->loading($userId, 5);ทำเครื่องหมายว่าอ่านข้อความผู้ใช้แล้ว ใช้ token จาก webhook
$line->message()->markAsRead($markAsReadToken);ดูเพดานส่งข้อความเดือนนี้
$line->message()->quota();ดูจำนวนข้อความที่ส่งไปแล้วเดือนนี้
$line->message()->quotaConsumption();จำนวน reply ที่ส่งสำเร็จในวันนั้น date เป็น yyyyMMdd
$line->message()->deliveryReply('20260919');$line->message()->deliveryPush('20260919');$line->message()->deliveryMulticast('20260919');$line->message()->deliveryBroadcast('20260919');ตรวจโครงสร้างข้อความสำหรับ reply ก่อนส่งจริง
$line->message()->validateReply($text);$line->message()->validatePush($text);$line->message()->validateMulticast($text);$line->message()->validateNarrowcast($text);$line->message()->validateBroadcast($text);ดูจำนวนหน่วย aggregation ที่ใช้เดือนนี้
$line->message()->aggregationInfo();ดึงรายชื่อหน่วย aggregation เดือนนี้ ใช้ $start จาก next เพื่อดึงหน้าถัดไป
$line->message()->aggregationList();
$line->message()->aggregationList(100, $next);$line->users()
ดึงโปรไฟล์ผู้ใช้ที่เพิ่มเพื่อนแล้ว และรายการ user ID ของเพื่อน
เมธอดในหมวดนี้:
$line->users();
$line->users()->profile($userId);
$line->users()->followers();
$line->users()->followers($next, 1000);ดึงชื่อ รูป และสถานะของผู้ใช้ที่เพิ่มเพื่อนแล้ว ใช้กับคนที่บล็อกไม่ได้
$line->users()->profile($userId);ดึงรายการ user ID ของเพื่อน ไม่ส่งพารามิเตอร์ LINE จะใช้ค่าเริ่มต้น (limit 300) ถ้ามีหน้าถัดไปให้ส่งค่า next จากรอบก่อนเป็น $start limit สูงสุด 1000
$line->users()->followers();
$line->users()->followers($next, 1000);$line->group()
ข้อมูลกลุ่มที่บอทเป็นสมาชิกอยู่
เมธอดในหมวดนี้:
$line->group();
$line->group()->summary($groupId);
$line->group()->membersCount($groupId);
$line->group()->membersIds($groupId);
$line->group()->membersIds($groupId, $next);
$line->group()->member($groupId, $userId);
$line->group()->leave($groupId);ดูชื่อและรูปกลุ่ม
$line->group()->summary($groupId);นับจำนวนสมาชิกในกลุ่ม
$line->group()->membersCount($groupId);ดึง user ID ของสมาชิก ถ้าเกิน 100 คนใช้ $start จาก next
$line->group()->membersIds($groupId);
$line->group()->membersIds($groupId, $next);ดึงโปรไฟล์สมาชิกในกลุ่ม
$line->group()->member($groupId, $userId);ให้บอทออกจากกลุ่ม
$line->group()->leave($groupId);$line->room()
ห้องแชทหลายคนที่บอทอยู่ ไม่มี API สรุปห้องแบบกลุ่ม
เมธอดในหมวดนี้:
$line->room();
$line->room()->memberCount($roomId);
$line->room()->memberIds($roomId);
$line->room()->memberIds($roomId, $next);
$line->room()->member($roomId, $userId);
$line->room()->leave($roomId);นับจำนวนสมาชิกในห้อง
$line->room()->memberCount($roomId);ดึง user ID ของสมาชิก ถ้าเกิน 100 คนใช้ $start จาก next
$line->room()->memberIds($roomId);
$line->room()->memberIds($roomId, $next);ดึงโปรไฟล์สมาชิกในห้อง
$line->room()->member($roomId, $userId);ให้บอทออกจากห้อง
$line->room()->leave($roomId);$line->richmenu()
สร้าง ผูก และจัดการริชเมนู อัปโหลด/ดาวน์โหลดรูปใช้ api-data.line.me
เมธอดในหมวดนี้:
$line->richmenu();
$line->richmenu()->create($richMenuObject);
$line->richmenu()->validate($richMenuObject);
$line->richmenu()->uploadImg($richMenuId, $path);
$line->richmenu()->downloadImg($richMenuId);
$line->richmenu()->getList();
$line->richmenu()->get($richMenuId);
$line->richmenu()->delete($richMenuId);
$line->richmenu()->setDefault($richMenuId);
$line->richmenu()->getDefaultList();
$line->richmenu()->cancelDefault();
$line->richmenu()->createAlias($aliasId, $richMenuId);
$line->richmenu()->updateAlias($aliasId, $richMenuId);
$line->richmenu()->getAlias($aliasId);
$line->richmenu()->getAliasList();
$line->richmenu()->deleteAlias($aliasId);
$line->richmenu()->userLink($userId, $richMenuId);
$line->richmenu()->usersLink([$userId1, $userId2], $richMenuId);
$line->richmenu()->getByUserId($userId);
$line->richmenu()->deleteByUserId($userId);
$line->richmenu()->deleteByUserIds([$userId1, $userId2]);
$line->richmenu()->validateBatch($operations);
$line->richmenu()->replaceBatch($operations);
$line->richmenu()->progressBatch($requestId);สร้างริชเมนูจากอ็อบเจ็กต์ตามเอกสาร LINE คืน richMenuId
$line->richmenu()->create($richMenuObject);ตรวจอ็อบเจ็กต์ก่อนสร้าง
$line->richmenu()->validate($richMenuObject);อัปโหลดรูป JPEG หรือ PNG $img เป็นพาธไฟล์หรือไบนารี ถ้าเป็น .png จะส่ง image/png ให้
$line->richmenu()->uploadImg($richMenuId, '/path/to/menu.png');ดาวน์โหลดรูปริชเมนู คืน content และ contentType
$line->richmenu()->downloadImg($richMenuId);ดึงรายการริชเมนูทั้งหมดของช่อง
$line->richmenu()->getList();ดูริชเมนูจาก ID
$line->richmenu()->get($richMenuId);ลบริชเมนู
$line->richmenu()->delete($richMenuId);ตั้งริชเมนูเริ่มต้นของบอท
$line->richmenu()->setDefault($richMenuId);ดู ID ริชเมนูเริ่มต้น
$line->richmenu()->getDefaultList();ยกเลิกริชเมนูเริ่มต้น
$line->richmenu()->cancelDefault();สร้าง alias สำหรับสลับแท็บริชเมนู
$line->richmenu()->createAlias('tab-a', $richMenuId);ให้ alias ชี้ไปริชเมนูอื่น
$line->richmenu()->updateAlias('tab-a', $otherRichMenuId);ดูข้อมูล alias
$line->richmenu()->getAlias('tab-a');ดึงรายการ alias ทั้งหมด
$line->richmenu()->getAliasList();ลบ alias
$line->richmenu()->deleteAlias('tab-a');ผูกริชเมนูกับผู้ใช้คนเดียว
$line->richmenu()->userLink($userId, $richMenuId);ผูกริชเมนูกับผู้ใช้หลายคน
$line->richmenu()->usersLink([$userId1, $userId2], $richMenuId);ดูริชเมนูที่ผูกกับผู้ใช้อยู่
$line->richmenu()->getByUserId($userId);ถอดริชเมนูออกจากผู้ใช้คนเดียว
$line->richmenu()->deleteByUserId($userId);ถอดริชเมนูออกจากผู้ใช้หลายคน
$line->richmenu()->deleteByUserIds([$userId1, $userId2]);ตรวจคำขอเปลี่ยน/ถอดริชเมนูเป็นชุดก่อนส่งจริง
$ops = [['type' => 'link', 'from' => $oldId, 'to' => $newId]];
$line->richmenu()->validateBatch($ops);เปลี่ยนหรือถอดริชเมนูเป็นชุด
$line->richmenu()->replaceBatch($ops);ดูสถานะงาน batch จาก requestId
$line->richmenu()->progressBatch($requestId);$line->audience()
สร้างและจัดการกลุ่มผู้รับสำหรับ narrowcast
เมธอดในหมวดนี้:
$line->audience();
$line->audience()->upload($description, false, '', $audiences);
$line->audience()->uploadByFile($description, $path);
$line->audience()->add($audienceGroupId, $audiences);
$line->audience()->addByFile($audienceGroupId, $path);
$line->audience()->click($description, $requestId, $clickUrl);
$line->audience()->imp($description, $requestId);
$line->audience()->rename($audienceGroupId, $description);
$line->audience()->get($audienceGroupId);
$line->audience()->delete($audienceGroupId);
$line->audience()->getList();
$line->audience()->getShared($audienceGroupId);
$line->audience()->getSharedList();$line->audience()->upload($description, $isIfaAudience = false, $uploadDescription = '', $audiences = [])
สร้าง audience จาก JSON ของ user ID หรือ IFA
$line->audience()->upload('แคมเปญ ก', false, '', [
['id' => $userId],
]);$line->audience()->uploadByFile($description, $file, $isIfaAudience = false, $uploadDescription = '')
สร้าง audience จากไฟล์ข้อความ หนึ่ง ID ต่อบรรทัด $file เป็นพาธหรือเนื้อไฟล์
$line->audience()->uploadByFile('แคมเปญ ก', '/path/to/ids.txt');เพิ่ม user ID หรือ IFA เข้า audience ที่มีอยู่ แบบ JSON
$line->audience()->add($audienceGroupId, [['id' => $userId]]);เพิ่มรายชื่อจากไฟล์เข้า audience ที่มีอยู่
$line->audience()->addByFile($audienceGroupId, '/path/to/ids.txt');สร้าง audience จากคนที่คลิกลิงก์ในข้อความ $requestId ของข้อความที่ส่งไป
$line->audience()->click('คลิกลิงก์', $requestId, 'https://example.com');สร้าง audience จากคนที่เห็นข้อความ
$line->audience()->imp('เห็นข้อความ', $requestId);เปลี่ยนชื่อ audience
$line->audience()->rename($audienceGroupId, 'ชื่อใหม่');ดูข้อมูล audience
$line->audience()->get($audienceGroupId);ลบ audience
$line->audience()->delete($audienceGroupId);$line->audience()->getList($page = 1, $size = 20, $description = '', $status = '', $includesExternalPublicGroups = true, $createRoute = '')
ดึงรายการ audience ของช่อง size สูงสุด 40
$line->audience()->getList();
$line->audience()->getList(1, 20, '', 'READY');ดูข้อมูล audience ที่แชร์จาก Business Manager
$line->audience()->getShared($audienceGroupId);$line->audience()->getSharedList($page = 1, $size = 20, $description = '', $status = '', $createRoute = '', $includesOwnedAudienceGroups = false)
ดึงรายการ audience ที่แชร์จาก Business Manager
$line->audience()->getSharedList();$line->insight()
วันที่ใช้รูปแบบ yyyyMMdd
เมธอดในหมวดนี้:
$line->insight();
$line->insight()->messageDelivery('20260919');
$line->insight()->followers('20260919');
$line->insight()->demographic();
$line->insight()->messageEvent($requestId);
$line->insight()->messageEventAggregation('promo_a', '20260901', '20260919');
$line->insight()->richmenuSummary($richMenuId, '20260901', '20260919');
$line->insight()->richmenuDaily($richMenuId, '20260901', '20260919');จำนวนข้อความที่ส่งสำเร็จในวันนั้น
$line->insight()->messageDelivery('20260919');จำนวนเพื่อนในวันนั้น
$line->insight()->followers('20260919');ข้อมูลประชากรของเพื่อน
$line->insight()->demographic();สถิติการโต้ตอบกับ narrowcast หรือ broadcast
$line->insight()->messageEvent($requestId);สถิติต่อหน่วย aggregation ของ push / multicast
$line->insight()->messageEventAggregation('promo_a', '20260901', '20260919');สถิติริชเมนูรวมในช่วงวันที่กำหนด
$line->insight()->richmenuSummary($richMenuId, '20260901', '20260919');สถิติริชเมนูแยกตามวัน
$line->insight()->richmenuDaily($richMenuId, '20260901', '20260919');$line->membership()
ฟีเจอร์ Membership ของบัญชีทางการ คือแผนที่ผู้ใช้สมัครจ่ายเงินกับเพจ ไม่ใช่รายชื่อสมาชิกในกลุ่มแชท
เมธอดในหมวดนี้:
$line->membership();
$line->membership()->subscription($userId);
$line->membership()->getList();
$line->membership()->userIds($membershipId);
$line->membership()->userIds($membershipId, $next, 1000);ดูว่าผู้ใช้อยู่แผนไหน และสถานะเป็นอย่างไร
$line->membership()->subscription($userId);ดึงรายการแผนที่บัญชีทางการเปิดไว้
$line->membership()->getList();ดึง user ID ของผู้ที่สมัครแผนนั้น limit สูงสุด 1000
$line->membership()->userIds($membershipId);
$line->membership()->userIds($membershipId, $next, 1000);$line->coupon()
สร้าง ดู และปิดคูปอง
เมธอดในหมวดนี้:
$line->coupon();
$line->coupon()->create($couponObject);
$line->coupon()->getList();
$line->coupon()->getList(['RUNNING']);
$line->coupon()->get($couponId);
$line->coupon()->close($couponId);สร้างคูปองใหม่ $coupon เป็นอ็อบเจ็กต์ตามเอกสาร LINE
$line->coupon()->create($couponObject);ดึงรายการคูปอง $status เช่น DRAFT RUNNING CLOSED
$line->coupon()->getList();
$line->coupon()->getList(['RUNNING']);ดูรายละเอียดคูปอง
$line->coupon()->get($couponId);ปิดคูปองไม่ให้ใช้ต่อ
$line->coupon()->close($couponId);$line->accountLink()
ออก token สำหรับผูกบัญชีบริการกับบัญชี LINE
เมธอดในหมวดนี้:
$line->accountLink();
$line->accountLink()->token($userId);ออก link token
$line->accountLink()->token($userId);หมวดนี้ไม่ใช่การล็อกอินผู้ใช้ (LINE Login คนละระบบ)
$line->oauth() และ $line->oauth2() ใช้โปรโตคอล OAuth 2.0 เพื่อออก / ตรวจ / เพิกถอน Channel Access Token ของบอท ให้แอปเรียก Messaging API ได้ โดยไม่ต้องคัดลอก token จาก Console ทุกครั้ง
เมธอดในหมวดนี้:
$line->oauth();
$line->oauth()->accessToken();
$line->oauth()->verify($accessToken);
$line->oauth()->revoke($accessToken);
$line->oauth2();
$line->oauth2()->token($jwt);
$line->oauth2()->verify($accessToken);
$line->oauth2()->tokens($jwt);
$line->oauth2()->revoke($accessToken);
$line->oauth2()->tokenV3();
$line->oauth2()->tokenV3($jwt);ออก Channel Access Token อายุประมาณ 30 วัน (short-lived) ผ่าน /v2/oauth
$line->oauth()->accessToken();ตรวจว่า token อายุสั้นหรือยาวจาก Console ยังใช้ได้หรือไม่
$line->oauth()->verify($accessToken);เพิกถอน token อายุสั้นหรือยาวจาก Console
$line->oauth()->revoke($accessToken);ออก Channel Access Token รุ่น 2.1 ด้วย JWT
$line->oauth2()->token($jwt);ตรวจว่า token รุ่น 2.1 ยังใช้ได้หรือไม่
$line->oauth2()->verify($accessToken);ดึง key ID ของ token รุ่น 2.1 ที่ยังใช้ได้ทั้งหมด
$line->oauth2()->tokens($jwt);เพิกถอน token รุ่น 2.1
$line->oauth2()->revoke($accessToken);ออก token แบบ stateless อายุ 15 นาที เพิกถอนไม่ได้ ไม่ส่ง JWT จะใช้ channel ID + secret จากตอนสร้าง Line
$line->oauth2()->tokenV3();
$line->oauth2()->tokenV3($jwt);Partner API (เช่น LINE Notification Message) ไม่ได้อยู่ในแพ็กเกจนี้
- สร้าง Messaging API Channel ที่ LINE Developers Console
- ใส่ค่าใน environment หรือ
.envของแอป อย่า commit token - ตั้ง webhook เป็น HTTPS ที่เข้าถึงได้
- ส่งข้อความจาก LINE แล้วเรียก
$line->message()->reply()ด้วยreplyToken - ลอง
$line->info()และ$line->users()->profile()กับ user ID จาก webhook
MIT
