Created
January 8, 2017 05:19
-
-
Save kiuyas/dfb0fd52a7f78e9ae6f3e282c90e7735 to your computer and use it in GitHub Desktop.
LINEのBotに応答させるためのPHPスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$json_string = file_get_contents('php://input'); | |
$jsonObj = json_decode($json_string); | |
$userId = $jsonObj->{"events"}[0]->{"source"}->{"userId"}; | |
$replyToken = $jsonObj->{"events"}[0]->{"replyToken"}; | |
$message = $jsonObj->{"events"}[0]->{"message"}->{"text"}; | |
error_log("-------------------------------------------------- hello, this is a test!"); | |
error_log(print_r($jsonObj, true)); | |
error_log("userId: " . $userId); | |
error_log("replyToken: " . $replyToken); | |
error_log("message: " . $message); | |
$post_data = array( | |
"replyToken" => $replyToken, | |
"messages" => array ( array("type" => "text", "text" => "Hello, World!")) | |
); | |
error_log("*** Following content will be posted. ***"); | |
error_log(print_r(json_decode(json_encode($post_data)), true)); | |
$ch = curl_init("https://api.line.me/v2/bot/message/reply"); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Authorization: Bearer [チャンネルアクセストークン※LINE Developerのページで確認]' | |
)); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
error_log("*** Result ***"); | |
error_log($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
流れとしては次のような感じなんだと思います。
(1) POSTリクエストでJSONを受け取る
(2) 返答内容をJSONで作る
(3) CURLなどを利用して(2)を https://api.line.me/v2/bot/message/reply へ送りつける。