teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

下書きと間違えて投稿してしまったため、全文を入れました

2020/01/31 14:31

投稿

kmtym1998
kmtym1998

スコア9

title CHANGED
@@ -1,1 +1,1 @@
1
- PHPでGoogleのOAuth2.0
1
+ PHPでGoogleのOAuth2.0、file_get_contets()が使えない
body CHANGED
@@ -6,6 +6,7 @@
6
6
  - [GoogleのOAuth2.0を使ってプロフィールを取得【PHP】](http://famirror.hateblo.jp/entry/2015/12/18/180000)
7
7
 
8
8
  ```php
9
+ //index.php
9
10
  <?php
10
11
      // アプリケーション設定
11
12
      define('CONSUMER_KEY', '<クライアントID>');
@@ -24,4 +25,63 @@
24
25
  ?>
25
26
 
26
27
  ```
27
- リダイレクトURIに指定したページが以下のPHPファイルです。
28
+ リダイレクトURIに指定したページが以下のPHPファイルです。
29
+
30
+ ```php
31
+ //display.php
32
+ <?php
33
+ // アプリケーション設定
34
+ define('CONSUMER_KEY', '<クライアントID>');
35
+ define('CONSUMER_SECRET', '<クライアント シークレット>');
36
+ define('CALLBACK_URL', 'http://localhost/test/oauth.php');
37
+ // URL
38
+ define('TOKEN_URL', 'https://accounts.google.com/o/oauth2/token');
39
+ define('INFO_URL', 'https://www.googleapis.com/oauth2/v1/userinfo');
40
+ $params = array(
41
+ 'code' => $_GET['code'],
42
+ 'grant_type' => 'authorization_code',
43
+ 'redirect_uri' => CALLBACK_URL,
44
+ 'client_id' => CONSUMER_KEY,
45
+ 'client_secret' => CONSUMER_SECRET,
46
+ );
47
+ // POST送信
48
+ $options = array('http' => array(
49
+ 'method' => 'POST',
50
+ 'content' => http_build_query($params)
51
+ ));
52
+ // アクセストークンの取得
53
+ $res = file_get_contents(TOKEN_URL, false, stream_context_create($options));
54
+ // レスポンス取得
55
+ $token = json_decode($res, true);
56
+ if(isset($token['error'])){
57
+ echo 'エラー発生';
58
+ exit;
59
+ }
60
+ $access_token = $token['access_token'];
61
+ $params = array('access_token' => $access_token);
62
+ // ユーザー情報取得
63
+ $res = file_get_contents(INFO_URL . '?' . http_build_query($params));
64
+ //表示
65
+ echo $res;
66
+ ?>
67
+ ```
68
+
69
+ # エラー
70
+ ```
71
+
72
+ Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ~display.php on line 22
73
+
74
+ 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
75
+
76
+ Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ~display.php on line 32
77
+
78
+ 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
79
+ ```
80
+ `index.php`にアクセスし、`display.php`にリダイレクトした際にこちらのエラーが出力されます。
81
+ 軽く調べてみたとろ、`php.ini`の`allow_url_include`に問題があるのかな?と思いましたが、自分が使用しているXFREEサーバでは、そこをいじることができません。
82
+
83
+ いろいろと調べてみましたが、いまいち原因がわからず手詰まってしまいました。
84
+ - file_get_contents()が警告なしで使える方法は他にあるか
85
+ - 以上で試している方法以外で、Googleアカウントのプロフィール情報を取得できる方法はあるか
86
+
87
+ こちらのいずれか、または両方についてご回答いただければ幸いです。