{"id":2015,"date":"2021-04-04T14:14:52","date_gmt":"2021-04-04T05:14:52","guid":{"rendered":"http:\/\/wp.ultimai.org\/?p=2015"},"modified":"2021-04-04T14:14:52","modified_gmt":"2021-04-04T05:14:52","slug":"line-messaging-api%e3%81%a7-php%e3%81%8b%e3%82%89-push%e3%83%a1%e3%83%83%e3%82%bb%e3%83%bc%e3%82%b8%e3%82%92%e9%80%81%e4%bf%a1%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/wp.ultimai.org\/?p=2015","title":{"rendered":"LINE Messaging API\u3067 php\u304b\u3089 Push\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\u3059\u308b"},"content":{"rendered":"<h3>1. LINE for Bussiness\u3092\u7528\u610f\u3059\u308b<\/h3>\n<p>LINE Messaging API\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f Line Developers \u306b\u767b\u9332\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/account.line.biz\/signup?redirectUri=https%3A%2F%2Fmanager.line.biz%2F\" target=\"_blank\" rel=\"noopener\">LINE Bussiness ID<\/a> \u3092\u4f5c\u308b<\/p>\n<p>\u6b21\u306b LINE Bussiness \u30a2\u30ab\u30a6\u30f3\u30c8\u3068LINE\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u9023\u643a\u3057\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/account.line.biz\/profile?redirectUri=https%3A%2F%2Fmanager.line.biz\" target=\"_blank\" rel=\"noopener\">\u9023\u643a\u3059\u308bURL<\/a><\/p>\n<h3>2. LINE Messaging API\u3092\u8a2d\u5b9a\u3059\u308b<\/h3>\n<p><a href=\"https:\/\/developers.line.biz\/console\/channel\/new?type=messaging-api&#038;provider\" rel=\"noopener\" target=\"_blank\">channel\u3092\u3064\u304f\u308b<\/a><\/p>\n<h3>Webhook\u3092\u8a2d\u5b9a<\/h3>\n<p>Webhook settings \u306e \u300cEdit\u300d \u3092\u30af\u30ea\u30c3\u30af\u3057\u3001\u516c\u958b\u3057\u3066\u3044\u308bWebhookURL\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h3>Channel access token\u3092\u6709\u52b9\u5316<\/h3>\n<p>Messaging API\u306e\u4e0b\u90e8\u306b\u3042\u308b\u300cChannel access token\u300d\u306e\u300cChannel access token (long-lived) \u300d\u304b\u3089\u300cIssue\u300d\u3092\u30af\u30ea\u30c3\u30af\u3002<\/p>\n<h3>\u3064\u304f\u3063\u305fchannel\u3092\u304a\u53cb\u9054\u767b\u9332\u3059\u308b<br \/>\n<\/h3>\n<p>Messaging API \u7ba1\u7406\u753b\u9762\u304b\u3089\u4f5c\u3063\u305f\u30c1\u30e3\u30f3\u30cd\u30eb\u306eQR\u30b3\u30fc\u30c9\u3092\u8868\u793a\u3057\u3066\u304a\u53cb\u9054\u767b\u9332\u3059\u308b<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\r\n<code>\r\n\r\n    \/\/ LINE Messaging API \u30d7\u30c3\u30b7\u30e5\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u308b\r\n    $LINE_PUSH_URL = \"https:\/\/api.line.me\/v2\/bot\/message\/push\";\r\n\r\n\r\n    $LINE_CHANNEL_ACCESS_TOKEN = 'ZXCVFDSA*******************'; \r\n\r\n    \/\/ Your user ID\r\n    \/\/ Messaging API \u7ba1\u7406\u753b\u9762\u3067\u78ba\u8a8d \u3053\u308c\u304f\u3089\u3044\u306a\u304c\u3044\u3084\u3064\r\n    $LINE_USER_ID = \"U3**********************\";\r\n\r\n    \/\/ \u9001\u4fe1\u3059\u308b\u30e1\u30c3\u30bb\u30fc\u30b8\r\n    $message_1 = \"\u3053\u3093\u306b\u3061\u306f API\";\r\n    $message_2 = \"PHP\u304b\u3089PUSH\u9001\u4fe1\\r\\n\u6539\u884c\u3057\u3066\uff12\u884c\u76ee\";\r\n\r\n    \/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u30d8\u30c3\u30c0\r\n    $header = [\r\n        'Authorization: Bearer ' . $LINE_CHANNEL_ACCESS_TOKEN,\r\n        'Content-Type: application\/json'\r\n    ];\r\n\r\n    \/\/ \u9001\u4fe1\u3059\u308b\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u4e0b\u6e96\u5099\r\n    $post_values = array(\r\n        [\r\n        \"type\" => \"text\",\r\n        \"text\" => $message_1\r\n        ],\r\n        [\r\n        \"type\" => \"text\",\r\n        \"text\" => $message_2\r\n        ]\r\n    );\r\n\r\n    \/\/ \u9001\u4fe1\u3059\u308b\u30c7\u30fc\u30bf\r\n    $post_data = [\r\n        \"to\" => $LINE_USER_ID,\r\n        \"messages\" => $post_values\r\n        ];\r\n\r\n    \/\/ \u30c7\u30d0\u30b0\u78ba\u8a8d\u7528\u306e\u30ed\u30b0\uff1a\u9001\u4fe1\u30c7\u30fc\u30bf\r\n    $file = '\/tmp\/post_data.txt';\r\n    file_put_contents($file, json_encode($post_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), FILE_APPEND);\r\n    file_put_contents($file, PHP_EOL.PHP_EOL, FILE_APPEND);\r\n\r\n    \/\/ cURL\u3092\u4f7f\u3063\u305f\u9001\u4fe1\u51e6\u7406\u306e\u6642\u306f true\r\n    \/\/ file_get_contents\u3092\u4f7f\u3063\u305f\u9001\u4fe1\u51e6\u7406\u306e\u6642\u306f false\r\n    $USE_CURL = true;\r\n\r\n    if ($USE_CURL) {\r\n        \/\/ cURL\u3092\u4f7f\u3063\u305f\u9001\u4fe1\u51e6\u7406\r\n        $curl = curl_init();\r\n        curl_setopt($curl, CURLOPT_URL, $LINE_PUSH_URL);\r\n        curl_setopt($curl, CURLOPT_POST, true);\r\n        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');\r\n        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);\r\n        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);\r\n        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));\r\n        $result = curl_exec($curl);\r\n        curl_close($curl);\r\n    }\r\n    else\r\n    {\r\n        \/\/ file_get_contents\u3092\u4f7f\u3063\u305f\u9001\u4fe1\u51e6\u7406\r\n        $context = stream_context_create(array(\r\n            'http' => array(\r\n                'method' => 'POST',\r\n                'header' => implode(\"\\r\\n\", $header),\r\n                'content'=>  json_encode($post_data),\r\n                'ignore_errors' => true\r\n            )\r\n        ));\r\n\r\n        $result = file_get_contents(\r\n            $LINE_PUSH_URL,\r\n            false,\r\n            $context\r\n            );\r\n    }\r\n\r\n    \/\/ \u30c7\u30d0\u30b0\u78ba\u8a8d\u7528\u306e\u30ed\u30b0\uff1a\u53d7\u4fe1\u30ec\u30b9\u30dd\u30f3\u30b9\r\n    $file = '\/tmp\/result.txt';\r\n    file_put_contents($file, $result, FILE_APPEND);\r\n    file_put_contents($file, PHP_EOL.PHP_EOL, FILE_APPEND);\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. LINE for Bussiness\u3092\u7528\u610f\u3059 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2015","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=\/wp\/v2\/posts\/2015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2015"}],"version-history":[{"count":1,"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=\/wp\/v2\/posts\/2015\/revisions"}],"predecessor-version":[{"id":2019,"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=\/wp\/v2\/posts\/2015\/revisions\/2019"}],"wp:attachment":[{"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.ultimai.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}