PHP初心者です!皆様の力をお貸しいただけるととても助かります。
今現在、Githubアカウントで自前サービスにログインを行うためのいわゆるソーシャルログイン機能
を実装しております。
そこで、Github側の認証を通し、ユーザ情報をjson形式で取得した後に、json_decodeを行い
SQLでDB内にid情報やemail情報を格納する処理を書いているところなのですが、json_decodeが上手いこと出来ない状況です。
詳細は以下の通りです。(一部)
Githubアカウントでログイン!機能の実装時json_decodeするとエラーを吐きnullで返って来る問題
<?php session_start(); require_once('vendor/autoload.php'); require_once '_config.php'; // 正式にlogin.phpから来たのか確認 if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } // 認証コードからアクセストークンを取得 $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); echo $token.'\n'; echo 'Successfully callbacked!!'.'\n'; // トークン使って認可した情報を取得 $user = $provider->getResourceOwner($token); //var_dump($user);//この時点でdecodeする前の情報の出力は問題なし この時点での出力結果→ 半角英数字たくさん\nSuccessfully callbacked!!\nobject(League\OAuth2\Client\Provider\GithubResourceOwner)#20 (2) { ["domain":protected]=> string(18) "https://github.com" ["response":protected]=> array(30) { ["login"]=> string(16) "aaaaaaaa" ["id"]=> int(12345678) ["avatar_url"]=> string(53) "https://avatars2.githubusercontent.com/u/1234567v=3" ["gravatar_id"]=> string(0) "" ["url"]=> string(45) "https://api.github.com/users/ユーザ名" ["html_url"]=> string(35) "https://github.com/ユーザ名" ["followers_url"]=> string(55) "https://api.github.com/users/ユーザ名/followers" ["following_url"]=> string(68) "https://api.github.com/users/ユーザ名/following{/other_user}" ["gists_url"]=> string(61) "https://api.github.com/users/ユーザ名/gists{/gist_id}" ["starred_url"]=> string(68) "https://api.github.com/users/ユーザ名/starred{/owner}{/repo}" ["subscriptions_url"]=> string(59) "https://api.github.com/users/ユーザ名/subscriptions" ["organizations_url"]=> string(50) "https://api.github.com/users/ユーザ名/orgs" ["repos_url"]=> string(51) "https://api.github.com/users/ユーザ名/repos" ["events_url"]=> string(62) "https://api.github.com/users/ユーザ名/events{/privacy}" ["received_events_url"]=> string(61) "https://api.github.com/users/ユーザ名/received_events" ["type"]=> string(4) "User" ["site_admin"]=> bool(false) ["name"]=> NULL ["company"]=> NULL ["blog"]=> string(0) "" ["location"]=> NULL ["email"]=> NULL ["hireable"]=> NULL ["bio"]=> NULL ["public_repos"]=> int(20) ["public_gists"]=> int(0) ["followers"]=> int(0) ["following"]=> int(0) ["created_at"]=> string(20) "2017-04-24T04:41:00Z" ["updated_at"]=> string(20) "2017-06-14T08:41:44Z" } } $result = json_decode($user, true); //var_dump($result);//ここでのエラー→Warning: json_decode() expects parameter 1 to be string, object given in //nullで返って来てしまう ------------------------------------------------------------------------------ 詳細は以上です。 どうぞ一つ宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/19 07:14