5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【LINE Messanging API】プレビュー画像がキャッシュのせいでうまく表示されないときの解決方法

Posted at

はじめに

LINEbot開発をしている際にハマったことと解決策のメモ。

【環境】

  • PHP(7.3.2)
    • ImageMagick(7.0.7-11)
  • Heroku
  • LINE Messaging API

課題

ユーザのメッセージに応じてサーバに動的に保存した画像をリプライするbotを作っている際に、リプライのプレビュー画像が前回のキャッシュのせいで正しい画像が表示されなかった。

before.php
$client->replyMessage(array(
    'replyToken' => $event['replyToken'],
    'messages' => array(
        array(
            'type' => 'image',
            'originalContentUrl' => 'https://' . $_SERVER["HTTP_HOST"] . '/images/' . $userid . 'out.png',
            'previewImageUrl' => 'https://' . $_SERVER["HTTP_HOST"] . '/images/' . $userid . 'out.png',
        ),
    ),
));

解決方法

.uniqid()を画像指定の時につけたらキャッシュが表示されずに、新たに生成された画像が表示された。

after.php
$client->replyMessage(array(
    'replyToken' => $event['replyToken'],
    'messages' => array(
        array(
            'type' => 'image',
            'originalContentUrl' => 'https://' . $_SERVER["HTTP_HOST"] . '/images/' . $userid . 'out.png?=' . uniqid(),
            'previewImageUrl' => 'https://' . $_SERVER["HTTP_HOST"] . '/images/' . $userid . 'out.png?=' . uniqid(),
        ),
    ),
));
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?