{"id":3269,"date":"2025-09-03T11:31:59","date_gmt":"2025-09-03T03:31:59","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=3269"},"modified":"2025-09-09T08:32:23","modified_gmt":"2025-09-09T00:32:23","slug":"line-messaging-api-sdk-for-php-%e5%ae%89%e8%a3%9d%e5%88%b0-synology-nas","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=3269","title":{"rendered":"LINE Messaging API SDK for PHP \u5b89\u88dd\u5230 synology NAS"},"content":{"rendered":"\n<p>\u4eca\u5929\u60f3\u4f7f\u7528\u4e4b\u524d\u4e32\u63a5\u7684 LINE \u6a5f\u5668\u4eba\u4f86\u8ce3eSIM \u767c\u73fe\u4e0d\u6703\u52d5\u4e86\uff0c\u539f\u4f86\u662f\u6539\u7248\u4e86<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><a href=\"https:\/\/github.com\/line\/line-bot-sdk-php\">https:\/\/github.com\/line\/line-bot-sdk-php<\/a><\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u9019\u662f\u5b98\u65b9\u7684 github \uff0c\u6709\u5927\u90e8\u5206\u7684\u6559\u5b78\u6587\u4ef6<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u63a5\u4e0b\u4f86\u767c\u73fe NAS \u4f3c\u4e4e\u7121\u6cd5\u4f7f\u7528\u5efa\u8b70\u7684\u5b89\u88dd\u65b9\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>composer require linecorp\/line-bot-sdk<\/code><\/pre>\n\n\n\n<p>\u800c\u4e14\u76ee\u524d\u8a72API \u9700\u8981 php 8.1 \u4ee5\u4e0a\u7684\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>Requirements\nPHP 8.1 or later<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u9019\u908a\u4f7f\u7528macbook \u4f86\u5148\u4e0b\u6307\u4ee4<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>mkdir line\ncd line\n\nvi composer.json\n\u5167\u5bb9\u586b\u5165\n{\n    \"require\": {\n        \"linecorp\/line-bot-sdk\": \"^11.0\",\n        \"line\/openapi\": \"^1.0\"\n    }\n}\n\u4f86\u6307\u5b9a\u7248\u672c(\u9019\u61c9\u8a72\u662f\u975e\u5fc5\u8981\u7684)\n\ncomposer require linecorp\/line-bot-sdk\n\n\u4e4b\u5f8c\u5c07\u6703\u5728line \u8cc7\u6599\u593e\u5167\u5f97\u5230\u4e00\u500bvendor\u7684\u8cc7\u6599\u593e<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.quantoyo.com\/wp-content\/uploads\/2025\/09\/image.png\" alt=\"\" class=\"wp-image-3270\" width=\"235\" height=\"119\" srcset=\"https:\/\/blog.quantoyo.com\/wp-content\/uploads\/2025\/09\/image.png 704w, https:\/\/blog.quantoyo.com\/wp-content\/uploads\/2025\/09\/image-300x153.png 300w, https:\/\/blog.quantoyo.com\/wp-content\/uploads\/2025\/09\/image-150x77.png 150w\" sizes=\"auto, (max-width: 235px) 100vw, 235px\" \/><\/figure>\n\n\n\n<p>\u5230\u6b64\u6700\u9ebb\u7169\u7684\u4e00\u6b65\u5df2\u7d93\u8655\u7406\u5b8c\u7562\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u4f86\u5230NAS \u4e0a\u5b89\u88ddphp 8.1 \u4ee5\u4e0a\u7684\u7248\u672c\uff0c\u4e26\u6307\u5b9a\u5230webhook \u7684\u670d\u52d9\u4e0a\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u9019\u662f\u7c21\u55ae\u7684lineHook.php\uff0c\u53ef\u4ee5\u76f4\u63a5\u62ff\u4f86\u4e32\u63a5\uff0cautoload.php \u6703\u5e6b\u5fd9\u8655\u7406\u5e7e\u4e4e\u6240\u6709\u4e32\u63a5\u7684\u6771\u897f\uff0c\u4e0b\u65b9use \u7684\u90e8\u5206\u9700\u8981\u6839\u64da\u9700\u6c42\u81ea\u884c\u52a0\u4e0a<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>&lt;?php\nrequire_once __DIR__ . '\/vendor\/autoload.php';\n\nuse GuzzleHttp\\Client;\nuse LINE\\Clients\\MessagingApi\\Configuration;\nuse LINE\\Clients\\MessagingApi\\Api\\MessagingApiApi;\nuse LINE\\Clients\\MessagingApi\\Model\\ReplyMessageRequest;\nuse LINE\\Clients\\MessagingApi\\Model\\TextMessage;\nuse LINE\\Clients\\MessagingApi\\Model\\ImageMessage;\nuse LINE\\Clients\\MessagingApi\\Model\\TemplateMessage;\nuse LINE\\Clients\\MessagingApi\\Model\\ButtonsTemplate;\nuse LINE\\Clients\\MessagingApi\\Model\\PostbackAction;\nuse LINE\\Clients\\MessagingApi\\Model\\ConfirmTemplate;\nuse LINE\\Clients\\MessagingApi\\Model\\URIAction;\nuse LINE\\Clients\\MessagingApi\\Model\\MessageAction;\nuse LINE\\Clients\\MessagingApi\\Model\\CarouselTemplate;\nuse LINE\\Clients\\MessagingApi\\Model\\CarouselColumn;\nuse LINE\\Constants\\MessageType;\n\n$channelSecret = \"\u4f60\u7684channelSecret\";\n$channelToken = \"\u4f60\u7684channelToken\";\n\n\/\/ \u8a2d\u5b9a HTTP \u5ba2\u6236\u7aef\u548c\u914d\u7f6e\n$client = new Client();\n$config = new Configuration();\n$config-&gt;setAccessToken($channelToken);\n$messagingApi = new MessagingApiApi($client, $config);\n\n\/\/ \u63a5\u6536\u4e26\u9a57\u8b49 webhook \u8acb\u6c42\n$signature = $_SERVER&#91;'HTTP_' . 'X_LINE_SIGNATURE'];\n$body = file_get_contents('php:\/\/input');\n\n\/\/ \u7c21\u55ae\u9a57\u8b49 signature (\u624b\u52d5\u6aa2\u67e5)\n$hash = hash_hmac('sha256', $body, $channelSecret, true);\n$signatureCheck = base64_encode($hash);\n\nif ($signature !== $signatureCheck) {\n    error_log(\"Invalid signature\");\n    http_response_code(400);\n    exit;\n}\n\n\/\/ \u89e3\u6790\u4e8b\u4ef6 (\u624b\u52d5\u8655\u7406 JSON)\n$events = json_decode($body, true);\n\nif (isset($events&#91;'events'])) {\n    foreach ($events&#91;'events'] as $event) {\n    \t$tmpMsg = \"\";\n        if ($event&#91;'type'] === 'message' &amp;&amp; $event&#91;'message']&#91;'type'] === 'text') { \/\/\u6587\u5b57\u8a0a\u606f\n            $replyToken = $event&#91;'replyToken'];\n            $text = $event&#91;'message']&#91;'text'];\n\t\t\n            $tmpMsg = \"\u4f60\u6240\u8f38\u5165\u7684: \".$text;\n            \/\/ \u6e96\u5099\u56de\u8986\u8a0a\u606f\n            $message&#91;] = new TextMessage(&#91;\n                'type' =&gt; 'text',\n                'text' =&gt; $tmpMsg\n            ]);\n\t\t        \n        \t\n            $request = new ReplyMessageRequest(&#91;\n                'replyToken' =&gt; $replyToken,\n                'messages' =&gt; $message\n            ]);\n\n            \/\/ \u767c\u9001\u56de\u8986\n            try {\n                $response = $messagingApi-&gt;replyMessage($request);\n                error_log(\"Reply succeeded\");\n            } catch (\\LINE\\Clients\\MessagingApi\\ApiException $e) {\n                error_log(\"Reply failed: \" . $e-&gt;getCode() . \" \" . $e-&gt;getResponseBody());\n            }\n        }\n        else  if ($event&#91;'type'] === 'postback') {\n            $replyToken = $event&#91;'replyToken'];\n            $data = $event&#91;'postback']&#91;'data']; \/\/ e.g. \"plan=A\"\n            $userId = $event&#91;'source']&#91;'userId'] ?? \"NO\"; \/\/ \u53d6\u5f97 userId\n            $message = array();\n                $replyText = \"\u4f60\u9078\u7684\u9078\u9805\".$data;\n                $message&#91;] = new TextMessage(&#91;\n                    'type' =&gt; 'text',\n                    'text' =&gt; $replyText\n                ]);\n            \n\n            $request = new ReplyMessageRequest(&#91;\n                'replyToken' =&gt; $replyToken,\n                'messages' =&gt; $message\n            ]);\n\n            try {\n                $response = $messagingApi-&gt;replyMessage($request);\n                error_log(\"Reply succeeded\");\n            } catch (\\LINE\\Clients\\MessagingApi\\ApiException $e) {\n                error_log(\"Reply failed: \" . $e-&gt;getCode() . \" \" . $e-&gt;getResponseBody());\n            }\n        }\n        else if($event&#91;'type'] === 'follow'){  \/\/\u52a0\u70ba\u597d\u53cb\u89f8\u767c\n            \/\/ \u6e96\u5099\u56de\u8986\u8a0a\u606f\n            $message = new TextMessage(&#91;\n                'type' =&gt; 'text',\n                'text' =&gt; '\u60a8\u597d\uff0c\u9019\u88e1\u662fQuantoShop eSIM \u81ea\u52a9\u6a5f\u5668\u4eba\\n\u8acb\u8f38\u5165\u60a8\u6240\u8981\u67e5\u8a62\u7684 Order ID'\n            ]);\n            $request = new ReplyMessageRequest(&#91;\n                'replyToken' =&gt; $replyToken,\n                'messages' =&gt; &#91;$message]\n            ]);\n\n            \/\/ \u767c\u9001\u56de\u8986\n            try {\n                $response = $messagingApi-&gt;replyMessage($request);\n                error_log(\"Reply succeeded\");\n            } catch (\\LINE\\Clients\\MessagingApi\\ApiException $e) {\n                error_log(\"Reply failed: \" . $e-&gt;getCode() . \" \" . $e-&gt;getResponseBody());\n            }\n        }\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u76ee\u524d\u9019\u500b\u7248\u672c\u6211\u53ef\u4ee5\u505a\u5230\u76f4\u63a5\u56de\u61c9\u3001\u4e3b\u52d5\u50b3\u9001\u8a0a\u606f\uff0c\u4f46\u662f\u514d\u8cbb\u7684\u4e3b\u52d5\u50b3\u9001\u4e00\u500b\u6708\u53ea\u6709200\u5247\u3002<\/p>\n\n\n\n<p>\u9644\u4e0a\u6211\u7684vendor\u8cc7\u6599\u593e\uff0c\u9019\u61c9\u8a72\u53ef\u4ee5\u76f4\u63a5\u62ff\u4f86\u7528\u3002<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-286e9f4b-9dad-4910-a3f8-167e60200434\" href=\"https:\/\/blog.quantoyo.com\/wp-content\/uploads\/2025\/09\/vendor.zip\">vendor<\/a><a href=\"https:\/\/blog.quantoyo.com\/wp-content\/uploads\/2025\/09\/vendor.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-286e9f4b-9dad-4910-a3f8-167e60200434\">\u4e0b\u8f09<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f Carousel \u53ef\u7528\u7684\u7bc4\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>function createCarouselMessage_Country($plans){\n    $message = array();\n    if(count($plans) == 0){\n        $message&#91;] = new TextMessage(&#91;\n            'type' => 'text',\n            'text' => '\u76ee\u524d\u6c92\u6709\u4efb\u4f55\u65b9\u6848'\n        ]);\n    }else{\n        $columnAry = array();\n        for($i = 0;$i &lt; count($plans); $i++){\n            $column = new CarouselColumn();\n            $column->setThumbnailImageUrl(\"https:\/\/quantoyo.com\/\u570b\u5bb6.png\");\n            $column->setImageBackgroundColor(\"#F0F0F0\");\n            $column->setTitle($plans&#91;$i]&#91;'location']);\n            $column->setText('\u8acb\u9078\u64c7\u65b9\u6848\u985e\u578b');\n            $column->setActions(&#91;\n                new PostbackAction(&#91;\n                    'type' => 'postback',\n                    'label' => \"\u6d41\u91cf\u578b\u65b9\u6848\",\n                    'data' => $plans&#91;$i]&#91;'locationCode']\n                ])\n            ]);\n            $columnAry&#91;] = $column;\n            if(count($columnAry) == 10){        \/\/Carousel \u6700\u591a10\u500b\n                break;\n            }\n        }\n        \/\/ \u5efa\u7acb Carousel Template\n        $carousel = new CarouselTemplate();\n        $carousel->settype('carousel');\n        $carousel->setColumns($columnAry);\n\n        \/\/ \u52a0\u5165\u8981\u767c\u9001\u7684\u8a0a\u606f\u9663\u5217\n        $message&#91;] = new TemplateMessage(&#91;\n            'type' => 'template',\n            'altText' => '\u8acb\u9078\u64c7\u570b\u5bb6',\n            'template' => $carousel\n        ]);\n    }\n    return $message;\n}<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f Template \u53ef\u7528\u7bc4\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>\nfunction createTemplateMessage_Country($plans){\n    $message = array();\n    if(count($plans) == 0){\n        $message&#91;] = new TextMessage(&#91;\n            'type' => 'text',\n            'text' => '\u76ee\u524d\u6c92\u6709\u4efb\u4f55\u65b9\u6848'\n        ]);\n    }else{\n        $locationMsgAry = array();\n        $plansMsgAry = array();\n        for($i = 0;$i &lt; count($plans); $i++){\n            $tmpPostback = new PostbackAction(&#91;\n                'type' => 'postback',\n                'label' => limitStr($plans&#91;$i]&#91;'location'],20),\n                'data' => $plans&#91;$i]&#91;'locationCode']\n            ]);\n            $plansMsgAry&#91;] = $tmpPostback;\n            if(count($plansMsgAry) == 4  || $i == count($plans) -1){\n                $locationMsgAry&#91;] = $plansMsgAry;\n                $plansMsgAry = array();\n            }\n        }\n        for($i = 0;$i &lt; count($locationMsgAry); $i++){\n            $message&#91;] = new TemplateMessage(&#91;\n                'type' => 'template',\n                'altText' => '\u8acb\u9078\u64c7\u570b\u5bb6',\n                'template' => new ButtonsTemplate(&#91;\n                    'type' => 'buttons',\n                    'title' => '\u570b\u5bb6\u9078\u64c7',\n                    'text' => '\u8acb\u9078\u64c7\u4f60\u8981\u7684\u570b\u5bb6\uff1a',\n                    'actions' => $locationMsgAry&#91;$i]\n                ])\n            ]);\n            if(count($message) > 4)break;\n        }\n    }\n    return $message;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u60f3\u4f7f\u7528\u4e4b\u524d\u4e32\u63a5\u7684 LINE \u6a5f\u5668\u4eba\u4f86\u8ce3eSIM \u767c\u73fe\u4e0d\u6703\u52d5\u4e86\uff0c\u539f\u4f86\u662f\u6539\u7248\u4e86<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[239,72,70,76,531,530],"class_list":["post-3269","post","type-post","status-publish","format-standard","hentry","category-technology","tag-api","tag-bot","tag-line","tag-php","tag-sdk","tag-530"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3269"}],"version-history":[{"count":5,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3269\/revisions"}],"predecessor-version":[{"id":3292,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3269\/revisions\/3292"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}