LINE Messaging APIを使用した開発をやります。
$ php composer.phar require linecorp/line-bot-sdk
$ php composer.phar require guzzlehttp/guzzle
こちらでは上手くいかない
require("vendor/autoload.php"); use LINE\LINEBot\Constant\HTTPHeader; use LINE\LINEBot\HTTPClient\CurlHTTPClient; use LINE\LINEBot; $channel_access_token = 'xxxx'; $channel_secret = 'xxxx'; $http_client = new CurlHTTPClient($channel_access_token); $bot = new LINEBot($http_client, ['channelSecret' => $channel_secret]); $signature = $_SERVER['HTTP_' . HTTPHeader::LINE_SIGNATURE]; $http_request_body = file_get_contents('php://input'); $events = $bot->parseEventRequest($http_request_body, $signature); $event = $events[0]; $reply_token = $event->getReplyToken(); $reply_text = $event->getText(); $bot->replyText($reply_token, $reply_text);
Githubを見ながら少し内容を変更します。SDKのバージョンによってライブラリのclass構成が変わっているようです。
https://github.com/line/line-bot-sdk-php
replyTokenは、{“events”}[0]に入っている。line-bot-sdk-phpのsampleを見ても全然わからなかったので、凄い時間がかったw
ini_set("display_errors", 1); error_reporting(E_ALL); require("vendor/autoload.php"); $jsonString = file_get_contents('php://input'); error_log($jsonString); $jsonObj = json_decode($jsonString); $message = $jsonObj->{"events"}[0]->{"message"}; $replyToken = $jsonObj->{"events"}[0]->{"replyToken"}; $client = new \GuzzleHttp\Client(); $config = new \LINE\Clients\MessagingApi\Configuration(); $config->setAccessToken(''); $messagingApi = new \LINE\Clients\MessagingApi\Api\MessagingApiApi( client: $client, config: $config, ); $message = new \LINE\Clients\MessagingApi\Model\TextMessage(['type' => 'text','text' => $message->{"text"}]); $request = new \LINE\Clients\MessagingApi\Model\ReplyMessageRequest([ 'replyToken' => $replyToken, 'messages' => [$message], ]); $response = $messagingApi->replyMessage($request);