This error is seems to come every time
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
Below are some of the lines of code:
$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);
The error is in second last line, this is generally a GitHub API through PHP.
It seems like you don't have permission to access the "following" list for the user specified.
HTTP/1.1 403 Forbidden in
I'm just testing that URL format in my browser now, it seems I'm unable to view the JSON for any user's "following" list besides my own.
try this
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result; } echo getSslPage('https://api.github.com/users/TheYkk/following');
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.