ที่ บ เช่า server ของ amazon เป็น cloud computer ไว้เข้าละก็อัพขึ้น server ubuntu อีกทีนึง
คราวนี้ผมใช้ไฟล์ php เขียน line notify ตามนี้
$Token = $_GET["Token"];
$message = $_GET["message "];
line_notify($Token, $message);
fucntion line_notify($Token, $message)
{
$lineapi = $Token; // ใส่ token key ที่ได้มา
$mms = trim($message); // ข้อความที่ต้องการส่ง
date_default_timezone_set("Asia/Bangkok");
$chOne = curl_init();
curl_setopt( $chOne, CURLOPT_URL, "https://notify-api.line.me/api/notify");
// SSL USE
curl_setopt( $chOne, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $chOne, CURLOPT_SSL_VERIFYPEER, 0);
//POST
curl_setopt( $chOne, CURLOPT_POST, 1);
curl_setopt( $chOne, CURLOPT_POSTFIELDS, "message=$mms");
curl_setopt( $chOne, CURLOPT_FOLLOWLOCATION, 1);
$headers = array( 'Content-type: application/x-www-form-urlencoded', 'Authorization: Bearer '.$lineapi.'', );
curl_setopt($chOne, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $chOne, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec( $chOne );
//Check error
if(curl_error($chOne))
{
echo 'error:' . curl_error($chOne);
}
else {
$result_ = json_decode($result, true);
echo "status : ".$result_['status']; echo "message : ". $result_['message'];
}
curl_close( $chOne );
}$Token = $_GET["Token"];
$message = $_GET["message "];
line_notify($Token, $message);
fucntion line_notify($Token, $message)
{
$lineapi = $Token; // ใส่ token key ที่ได้มา
$mms = trim($message); // ข้อความที่ต้องการส่ง
date_default_timezone_set("Asia/Bangkok");
$chOne = curl_init();
curl_setopt( $chOne, CURLOPT_URL, "https://notify-api.line.me/api/notify");
// SSL USE
curl_setopt( $chOne, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $chOne, CURLOPT_SSL_VERIFYPEER, 0);
//POST
curl_setopt( $chOne, CURLOPT_POST, 1);
curl_setopt( $chOne, CURLOPT_POSTFIELDS, "message=$mms");
curl_setopt( $chOne, CURLOPT_FOLLOWLOCATION, 1);
$headers = array( 'Content-type: application/x-www-form-urlencoded', 'Authorization: Bearer '.$lineapi.'', );
curl_setopt($chOne, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $chOne, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec( $chOne );
//Check error
if(curl_error($chOne))
{
echo 'error:' . curl_error($chOne);
}
else {
$result_ = json_decode($result, true);
echo "status : ".$result_['status']; echo "message : ". $result_['message'];
}
curl_close( $chOne );
}
เวลาเรียกใช้ผมเรียกใช้ผ่าน ajax ใน localhost ของผม (xampp) รันได้ปกติส่งได้แต่พอขึ้น host จริงดัน error 500 ใน console ของ F12
พอผมเปลี่ยนมาใช้
define(‘LINE_API’,”https://notify-api.line.me/api/notify");
$token = “”; //ใส่Token ที่copy เอาไว้
$str = “Hello”; //ข้อความที่ต้องการส่ง สูงสุด 1000 ตัวอักษร
$res = notify_message($str,$token);
print_r($res);
function notify_message($message,$token){
$queryData = array(‘message’ => $message);
$queryData = http_build_query($queryData,’’,’&’);
$headerOptions = array(
‘http’=>array(
‘method’=>’POST’,
‘header’=> “Content-Type: application/x-www-form-urlencoded\r\n”
.”Authorization: Bearer “.$token.”\r\n”
.”Content-Length: “.strlen($queryData).”\r\n”,
‘content’ => $queryData
),
);
$context = stream_context_create($headerOptions);
$result = file_get_contents(LINE_API,FALSE,$context);
$res = json_decode($result);
return $res;
}
ดันใช้งานได้ เป็นเพราะอะไรครับ วิธีการเขียน php ไม่รองรับหรือว่ายังไง ตอนนี้ผมเขียน คำนวนเฉยๆแล้วส่งค่าไป js ก็ error 500 เหมือนเดิม
//edit ล่าสุดโค้ดคำนวนใช้ได้แล้วครับชื่อ table ใน php ไม่ตรงกัน ตัวเล็กตัวใหญ่ แต่ยังสงสัยเรื่อง line อยู่ครับ