如何使用PHP SDK获取LINE用户mid?

How to get LINE user mid using PHP SDK?

我使用 LINE BOT API 试用版 SDK PHP (https://github.com/line/line-bot-sdk-php)。

但是,对于这种方法:

$res = $bot->sendText(['TARGET_MID'], 'Message');

如何获取用户的 MID 以向他们发送消息?

感谢您的帮助。

1) 获得交互式用户 mid 的一种非常快速的方法(实际上更有趣)是注册回调 URL 根据 API instructions,然后将 POST 数据捕获到 URL,如下所示:

// /callback/index.php
<?php
$postdata = file_get_contents("php://input");
@file_get_contents('https://'.$_SERVER['SERVER_NAME'].'/LINE/' . json_encode($postdata));

接下来,扫描频道控制台中的二维码并将您的试用机器人添加到您的 LINE 帐户。完成后,快速发送 "Hello!" 文本。

然后您可以根据需要将 POST 数据保存到文本文件中,或者您可以检查服务器日志。例如,您可能会看到类似这样的内容:

163.128.118.223 - - [03/Sep/2016:07:25:25 -0700] "POST /line/callback/ HTTP/1.1" 200 - "-" "ChannelEventDispatcher/1.0"
106.152.218.107 - - [03/Sep/2016:07:25:25 -0700] "GET /LINE/{\"result\":[{\"content\":{\"toType\":1,\"createdTime\":1472114754839,\"from\":\"ub7dbd4a12c322f6c0117773d739c55a4\",\"location\":null,\"id\":\"4357194057879\",\"to\":[\"u2b6a4ba287028dee7291122094dac827\"],\"text\":\"Hello!\",\"contentMetadata\":{\"AT_RECV_MODE\":\"2\",\"SKIP_BADGE_COUNT\":\"true\"},\"deliveredTime\":0,\"contentType\":1,\"seq\":null},\"createdTime\":1472912724868,\"eventType\":\"138311609000106301\",\"from\":\"u236d23c2e36bd87217655609a1c31cb8\",\"fromChannel\":1241102815,\"id\":\"WB1519-3102846635\",\"to\":[\"u2b6a4ba287028dee7291122094dac827\"],\"toChannel\":1462261375}]} HTTP/1.1" 404 15 "-" "-"

\"from\":\"ub7dbd4a12c322f6c0117773d739c55a4\" 是相关部分。


2) 如果您想开始使用 receiving messages,您可以像这样作为回调脚本开始。只需向您的 BOT 发送消息 'mid',它应该会回复您的 mid.

这是我用 signature verification 为您制作的起始回调脚本。

// /callback/index.php
<?php
// Show all errors for testing
error_reporting(E_ALL);

// SDK is installed via composer
require_once __DIR__ . "/includes/vendor/autoload.php";

use LINE\LINEBot;
use LINE\LINEBot\HTTPClient\GuzzleHTTPClient;

// Set these
$config = [
    'channelId' => LINE_CHANNEL_ID,
    'channelSecret' => LINE_CHANNEL_SECRET,
    'channelMid' => LINE_CHANNEL_MID,
];
$sdk = new LINEBot($config, new GuzzleHTTPClient($config));

$postdata = @file_get_contents("php://input");
$messages = $sdk->createReceivesFromJSON($postdata);

// Verify the signature
// REF: http://line.github.io/line-bot-api-doc/en/api/callback/post.html#signature-verification
$sigheader = 'X-LINE-ChannelSignature';
// REF: 
$signature = @$_SERVER[ 'HTTP_'.strtoupper(str_replace('-','_',$sigheader)) ];
if($signature && $sdk->validateSignature($postdata, $signature)) {
    // Next, extract the messages
    if(is_array($messages)) {
        foreach ($messages as $message) {
            if ($message instanceof LINEBot\Receive\Message\Text) {
                $text = $message->getText();
                if ($text == "mid") {
                    $fromMid = $message->getFromMid();

                    // Send the mid back to the sender and check if the message was delivered
                    $result = $sdk->sendText([$fromMid], 'mid: ' . $fromMid);
                    if(!$result instanceof LINE\LINEBot\Response\SucceededResponse) {
                        error_log('LINE error: ' . json_encode($result));
                    }
                } else {
                    // Process normally, or do nothing
                }
            } else {
                // Process other types of LINE messages like image, video, sticker, etc.
            }
        }
    } // Else, error
} else {
    error_log('LINE signatures didn\'t match');
}