やりたいこと
PHPを使って認証を受けたユーザのGoogleアカウント情報を取得したい。
とりあえず、認証を受ける→基本プロフィール情報の出力 を目指している。
困っていること
以下のサイトを参考に、Googleアカウントを用いたユーザ認証の実装を試していました。
php
1//index.php 2<?php 3 // アプリケーション設定 4 define('CONSUMER_KEY', '<クライアントID>'); 5 define('CALLBACK_URL', '<リダイレクトURI>'); 6 // URL 7 define('AUTH_URL', 'https://accounts.google.com/o/oauth2/auth'); 8 9 $params = array( 10 'client_id' => CONSUMER_KEY, 11 'redirect_uri' => CALLBACK_URL, 12 'scope' => 'https://www.googleapis.com/auth/userinfo.profile email', 13 'response_type' => 'code', 14 ); 15 // 認証ページにリダイレクト 16 header("Location: " . AUTH_URL . '?' . http_build_query($params)); 17?> 18
リダイレクトURIに指定したページが以下のPHPファイルです。
php
1//display.php 2<?php 3// アプリケーション設定 4define('CONSUMER_KEY', '<クライアントID>'); 5define('CONSUMER_SECRET', '<クライアント シークレット>'); 6define('CALLBACK_URL', 'http://localhost/test/oauth.php'); 7// URL 8define('TOKEN_URL', 'https://accounts.google.com/o/oauth2/token'); 9define('INFO_URL', 'https://www.googleapis.com/oauth2/v1/userinfo'); 10$params = array( 11 'code' => $_GET['code'], 12 'grant_type' => 'authorization_code', 13 'redirect_uri' => CALLBACK_URL, 14 'client_id' => CONSUMER_KEY, 15 'client_secret' => CONSUMER_SECRET, 16); 17// POST送信 18$options = array('http' => array( 19 'method' => 'POST', 20 'content' => http_build_query($params) 21)); 22// アクセストークンの取得 23$res = file_get_contents(TOKEN_URL, false, stream_context_create($options)); 24// レスポンス取得 25$token = json_decode($res, true); 26if(isset($token['error'])){ 27 echo 'エラー発生'; 28 exit; 29} 30$access_token = $token['access_token']; 31$params = array('access_token' => $access_token); 32// ユーザー情報取得 33$res = file_get_contents(INFO_URL . '?' . http_build_query($params)); 34//表示 35echo $res; 36?>
エラー
Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ~display.php on line 22 Warning: file_get_contents(https://accounts.google.com/o/oauth2/token): failed to open stream: no suitable wrapper could be found in ~display.php on line 22 Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ~display.php on line 32 Warning: file_get_contents(https://www.googleapis.com/oauth2/v1/userinfo?): failed to open stream: no suitable wrapper could be found in ~display.php on line 32
index.php
にアクセスし、display.php
にリダイレクトした際にこちらのエラーが出力されます。
軽く調べてみたとろ、php.ini
のallow_url_include
に問題があるのかな?と思いましたが、自分が使用しているXFREEサーバでは、そこをいじることができません。
いろいろと調べてみましたが、いまいち原因がわからず手詰まってしまいました。
- file_get_contents()が警告なしで使える方法は他にあるか
- 以上で試している方法以外で、Googleアカウントのプロフィール情報を取得できる方法はあるか
こちらのいずれか、または両方についてご回答いただければ幸いです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/31 15:07
2020/01/31 15:14
2020/01/31 15:34
2020/01/31 15:40
退会済みユーザー
2020/01/31 16:24
退会済みユーザー
2020/01/31 16:26
2020/02/01 11:16
2020/02/01 14:47
2020/02/01 14:48
2020/02/01 20:47 編集
2022/02/08 14:45
2022/02/08 14:46
2022/02/09 01:13 編集