uzullaがブログ

uzullaがブログです。

How to use LINE Messaging API with PHP sdk in an hour.

this entry for beginner.

https://developers.line.me/messaging-api/overview

github.com

if you are expert, please see line-bot-sdk-php's examples. that is more better(more strict).

requirement

  • Public web server
  • need https
  • PHP version >= 5.6
  • curl extention
  • Composer

registration Developer trial

https://developers.line.me/

get tokens

You need to get channel token and channnel secret.

goto

https://developers.line.me/

and click channels at header right side.

you will see Channels basic infomation.

if you get wrong bot account?

if you try "TRIAL_BOT" before, open bellow url and click account list.

https://business.line.me/

then, open your developers trial bot account's Messaging API's LINE DEVELOPER button.

Okey, create your project

example file tree.

/project_root/
             /wwwroot/ # your httpd document root dir
                     /index.html # not require
                     /callback.php
             /vendor # will create by composer
             /composer.* # will create by composer

these are example. you can change wwwroot to htdocs,public and other as you like.

install sdk

$ mkdir project_root
$ cd project_root
$ wget https://getcomposer.org/composer.phar
$ php composer.phar require linecorp/line-bot-sdk

# will generate composer.json, composer.lock, vendor/

create callback.php at project_root/wwwroot/

this is sample echo bot.

<?php // callback.php
define("LINE_MESSAGING_API_CHANNEL_SECRET", 'your channel secret');
define("LINE_MESSAGING_API_CHANNEL_TOKEN", 'your channel token');

require __DIR__."/../vendor/autoload.php";

$bot = new \LINE\LINEBot(
    new \LINE\LINEBot\HTTPClient\CurlHTTPClient(LINE_MESSAGING_API_CHANNEL_TOKEN),
    ['channelSecret' => LINE_MESSAGING_API_CHANNEL_SECRET]
);

$signature = $_SERVER["HTTP_".\LINE\LINEBot\Constant\HTTPHeader::LINE_SIGNATURE];
$body = file_get_contents("php://input");

$events = $bot->parseEventRequest($body, $signature);

foreach ($events as $event) {
    if ($event instanceof \LINE\LINEBot\Event\MessageEvent\TextMessage) {
        $reply_token = $event->getReplyToken();
        $text = $event->getText();
        $bot->replyText($reply_token, $text);
    }
}

echo "OK";

please edit your channel secret, your channel token to your tokens.

register webhook URL(callback URL)

set your callback.php url to webhook URL.

you can edit webhook URL at Channels basic infomation.

open bellow url and click channnels (same as get token).

https://developers.line.me/

TEST!

follow your bot account, and send a text.

echo bot will echo back same text.

(sticker(and other) message is not normal text message, this echo bot will ignore.)

if you got any reply

at first, CHECK YOUR ACCESS_LOG and ERROR LOG! (IMPORTANT!!!)

You can not trust access from webhook from line?

this code will dump any request post body to /tmp/dump.txt.

<?php
ob_start();
$raw = file_get_contents('php://input');
var_dump(json_decode($raw,1));
$raw = ob_get_clean();
file_put_contents('/tmp/dump.txt', $raw."\n=====================================\n", FILE_APPEND);

echo "OK";

FYI: webhook access is not encoded by application/x-www form-urlencoded, so $_POST will blank.

SSL Error

SSL certificate problem: unable to get local issuer certificate

this is cause by YOUR php settings.

you need set curl ca certification

please google with 'curl.cainfo php.ini'.

done!

that's all. very simple.

see also

if you want to listen other messaging api events, plz see SDK example/KitchenSink.

enjoy!

for japanese

ここ数日のエントリをみてくれ。