|
|
|
[ช่วยด้วยค่ะ!] Line@bot php เซฟรูปลงดาต้าเบสยังไงคะ |
|
|
|
|
|
|
|
พอดีเพิ่งลองเขียน message API ในไลน์บอทค่ะ แล้วติดตรงเซฟรูปภาพ ทำยังไงให้เซฟรูปจากไลน์เข้าไปใน DAtabase ได้หรอคะ
ติดอยู่หลายวันแล้วไม่ได้ซักที T_T
Code (PHP)
$access_token = 'MzKjoa6jqMRAggO7UdrFd12tcYkR1YUzfXPVYINbZkYd9ZRYRfnvrBqsHDNSO1u2AvpldcpFH4aYh+dgPpbE73frQ3O9I32//Xm5BsDxpvfLOz51gxm4090U3Vs8/rY0jHA8uKj7PcssmgElH/hDnAdB04t89/1O/w1cDnyilFU=';
$content = file_get_contents('php://input');//php://input は、POST の生データの読み込みを 許可します。http://ivystar.jp/programming/php/what-is-phpinput/
// Parse JSON
$events = json_decode($content, true);
date_default_timezone_set('Asia/Tokyo');
if (!is_null($events['events'])) { //is_null — 変数が NULL かどうか調べる。var が null の場合に TRUE、 それ以外の場合に FALSE を返します。
if ($events['events'][0]['type'] == 'message'/* && $event['message']['type'] == 'text'*/) {
if($events['events'][0]['message']['type'] == 'image'){
$message_id= $events['events'][0]['message']['id'];
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient($access_token);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => '50790bf0d5ecfcf0f4b08de2c24c5ab6']);
$response = $bot->getMessageContent($message_id);
if ($response->isSucceeded()) {
$tempfile = tmpfile();
fwrite($tempfile, $response->getRawBody());
$text = sys_get_temp_dir();
} else {
error_log($response->getHTTPStatus() . ' ' . $response->getRawBody());
}
}
$replyToken = $events['events'][0]['replyToken'];
$messages = [
'type' => 'text',
'text' => $text
];
$url = 'https://api.line.me/v2/bot/message/reply';
$data = [
'replyToken' => $replyToken,
'messages' => [$messages],
];
$post = json_encode($data);
$headers = array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result . "\r\n";
}
}
echo "OK";
ตรงโค้ดช่วง
Code (PHP)
elseif($events['events'][0]['message']['type'] == 'image'){
$message_id= $events['events'][0]['message']['id'];
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient($access_token);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => '50790bf0d5ecfcf0f4b08de2c24c5ab6']);
$response = $bot->getMessageContent($message_id);
if ($response->isSucceeded()) {
$tempfile = tmpfile();
fwrite($tempfile, $response->getRawBody());
$text = sys_get_temp_dir();
} else {
error_log($response->getHTTPStatus() . ' ' . $response->getRawBody());
}
}
ที่เอาไว้เซฟรูปผิดตรงไหนเหรอคะ ทำไมรูปไม่เซฟลงในเครื่องคะ ทั้งๆที่รับ message จากuser มาแล้ว
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2017-02-04 08:08:46
|
|
|
|
|
Date :
2017-02-03 15:59:18 |
By :
nutmut |
View :
6288 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ให้ดู LINEBot.ImagemapMessageBuilder ครับ
Code (PHP)
$message = new MultiMessageBuilder();
// get imagemap params here as a $response
$richMessageUrl = $response['base_url'];
$imagemapMessageBuilder = new ImagemapMessageBuilder(
$richMessageUrl,
$response['alt_text'],
new BaseSizeBuilder(1040, 1040),
$response['actions']
);
$message->add( $imagemapMessageBuilder );
$message->add( new TextMessageBuilder( 'Testing Message' ) );
$resp = $bot->replyMessage(
$recipient,
$message
);
|
|
|
|
|
Date :
2017-02-08 10:00:02 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ทราบว่าตอนนี้ทำได้หรือยังคะอยากจะสอบถามวิธีการทำค่ะ
|
|
|
|
|
Date :
2017-11-09 11:28:49 |
By :
Loser_Bow |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมลองใช้โค้ดประมาณนี้ ก็ได้นะครับ
Code (PHP)
$response = $bot->getMessageContent($event['message']['id']);
if ($response->isSucceeded()) {
$dataBinary = $response->getRawBody();
//chdir('admins');
$fileFullSavePath = 'ชื่อรูปภาพ.jpg';
file_put_contents($fileFullSavePath,$dataBinary);
}
|
|
|
|
|
Date :
2018-09-19 23:33:22 |
By :
ordinary04 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|