Skip to content

Instantly share code, notes, and snippets.

@tickstudiu
Forked from aeknarinamn/LINEBOT.PHP
Created May 29, 2019 10:02
Show Gist options
  • Save tickstudiu/474f9b50087bdf29dac2cc76140ed9ac to your computer and use it in GitHub Desktop.
Save tickstudiu/474f9b50087bdf29dac2cc76140ed9ac to your computer and use it in GitHub Desktop.
LINE BOT EXAMPLE PHP EP1
<?php
/*Get Data From POST Http Request*/
$datas = file_get_contents('php://input');
/*Decode Json From LINE Data Body*/
$deCode = json_decode($datas,true);
file_put_contents('log.txt', file_get_contents('php://input') . PHP_EOL, FILE_APPEND);
$replyToken = $deCode['events'][0]['replyToken'];
$messages = [];
$messages['replyToken'] = $replyToken;
$messages['messages'][0] = getFormatTextMessage("เอ้ย ถามอะไรก็ตอบได้");
$encodeJson = json_encode($messages);
$LINEDatas['url'] = "https://api.line.me/v2/bot/message/reply";
$LINEDatas['token'] = "WMOsQ/4hWzaWxHAtvHw7H36SYWtU2RLT87CeKZk674MrmfcenRwApr+/9BIL1HkuWI9h3e8aDuQ6siofBjQ/rVh49uQEpz8r5It/hnguWF2Vd91lEBWHyQaYuNDvEfkruzMBbLrTXK5cs9wvmKhR/1GUYhWQfeY8sLGRXgo3xvw=";
$results = sentMessage($encodeJson,$LINEDatas);
/*Return HTTP Request 200*/
http_response_code(200);
function getFormatTextMessage($text)
{
$datas = [];
$datas['type'] = 'text';
$datas['text'] = $text;
return $datas;
}
function sentMessage($encodeJson,$datas)
{
$datasReturn = [];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $datas['url'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $encodeJson,
CURLOPT_HTTPHEADER => array(
"authorization: Bearer ".$datas['token'],
"cache-control: no-cache",
"content-type: application/json; charset=UTF-8",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$datasReturn['result'] = 'E';
$datasReturn['message'] = $err;
} else {
if($response == "{}"){
$datasReturn['result'] = 'S';
$datasReturn['message'] = 'Success';
}else{
$datasReturn['result'] = 'E';
$datasReturn['message'] = $response;
}
}
return $datasReturn;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment