[開発備忘録] LINEログイン for Javascript #3 PHPプログラミング

2025/07/18

PHP 開発 備忘録

t f B! P L
eyecatch Javascriptバージョンのみだと、セキュリティが不安な人もいるかと思うので、 サーバーサイド版のPHPバージョンも作っておきました。 githubを更新しているので、今回は、内容の解説をしておきます。

全体構成

基本的な構成は、Javascriptと同じで、以下のようなフローでLINEログインは、処理をして行きます。 ※LINE Developperの管理画面説明は完了している状態でスタートします。 (callbackURLなどは、もれなく設定しておくようにしましょう。)
1. LINEログインボタンの構築 (レギュレーションを守ればCSSのみの対応で十分) 2. 認証コードを取得するための、URL生成(ボタンクリックのイベントに設置 : JS) 3. callback.phpで受け取る。(ここからPHP処理) 4. access_tokenを取得するための、token.phpを実行 5. 取得したid_tokenを元に、verify.phpを実行(ここでメールが取得できます。) 6. access_tokenを元に、profile.phpを実行(名前、プロフィール、userIDなどが取得できます。)

token.php

<?php class Token{ public $datas = []; private $url = "https://api.line.me/oauth2/v2.1/token"; function __construct($options=[]){ $this->datas = $this->access_token( $options["code"], $options["client_id"], $options["channel_secret"], $options["callback_url"], ); } // Token取得 private function access_token($code=null, $client_id=null, $channel_secret=null, $callback_url=null){ $postData = http_build_query([ 'grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $client_id, 'client_secret' => $channel_secret, 'redirect_uri' => $callback_url, ]); $opts = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => $postData, ] ]; $context = stream_context_create($opts); $result = file_get_contents($this->url, false, $context); $response = json_decode($result, true); return $response; } }

解説

送信クエリを間違えなければ、比較的シンプルなAPIデータ取得方式で、リターンがもらえます。

verify.php

<?php class Verify{ public $datas = []; private $url = 'https://api.line.me/oauth2/v2.1/verify'; function __construct($options=[]){ $this->datas = $this->get_verify($options["id_token"], $options["client_id"], $options["access_token"]); } // Profile取得 private function get_verify($id_token=null, $client_id=null, $access_token=null) { $id_token = $id_token; $client_id = $client_id; if(!$id_token || !$client_id){return;} $postData = http_build_query([ 'id_token' => $id_token, 'client_id' => $client_id, ]); $opts = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => $postData, ] ]; $context = stream_context_create($opts); $result = file_get_contents($this->url, false, $context); return json_decode($result, true); } }

解説

id_tokenとclient_idのみで取得できてしまうんですが、このverifyで取得するデータで重要なのは、 emailのみです、profileでは、メールアドレスが取得できないので、このverifyで取得しましょう。

profile.php

<?php class Profile{ public $datas = []; private $url = 'https://api.line.me/v2/profile'; function __construct($options=[]){ $this->datas = $this->get_profile($options["access_token"]); } private function get_profile($access_token=null) { $opts = [ 'http' => [ 'header' => 'Authorization: Bearer ' . $access_token ] ]; $context = stream_context_create($opts); $result = file_get_contents($this->url, false, $context); return json_decode($result, true); } }

解説

プロフィールは、名前や、自己紹介文章などが取得でき、チャンネル固有のuserIdが取得できるので、サービスのuuidとして使用するのがいいでしょう。

あとがき

Javascriptとほぼ同じ処理なので、あまり説明もいらないかと思いましたが、とりあえずコピペでも使えると思ったので、掲載しておきました。 PHPで実装する事で、callback以降のデータは、サーバーサイドで処理するので、ぶっこぬかれる心配はありません。 実際にサイトに入れてみると、ボタン1回クリックでログインまでできてしまうので、非常に便利ですね。 裏では、データ管理をしっかりとしなければいけないですけどね。

人気の投稿

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。

ブログ アーカイブ