【发布时间】:2019-01-19 02:46:57
【问题描述】:
这个错误似乎每次都会出现
file_get_contents(https://api.github.com/users/AnyUsername/following): failed
to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in
C:\xampp\htdocs\Github\test.php on line 40
下面是一些代码行:
$data = array(
'client_id' => 'a7f10b4ef02b11843ae7',
'client_secret' => '89b227cbb3705099c7281ba367cfc5f868ea4f4b',
'redirect_uri'=>'http://localhost/Github/test.php',
'code'=>$code,
);
$post=http_build_query($data);
$url="https://github.com/login/oauth/access_token?".$post;
$contents=file_get_contents($url);
$explode1 = explode('access_token=', $contents);
$explode2 = explode('&scope=user', $explode1[1]);
$access_token = $explode2[0];
$opts = [ 'http' => [
'method' => 'GET',
'header' => [ 'User-Agent: PHP']
]
];
//fetching user data
$url = "https://api.github.com/user?access_token=".$access_token;
$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);
$user_data = json_decode($data, true);
$_SESSION['user'] = $user_data['login'];
$_SESSION['email']=$user_data['email'];
$img=$user_data['avatar_url'];
$name=$user_data['name'];
//fetching user following
$url="https://api.github.com/users/".$_SESSION['user']."/following";
$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);
$user_data = json_decode($data, true);
错误在倒数第二行,一般是GitHub API 通过 PHP。
【问题讨论】: