2017-03-26 4 views
0

Github Api로이 Api 호출에서 데이터를 가져 오려고합니다. 그러나 내가 시도하고 그것에서 데이터를 얻을 때마다 나는 아래의 오류가 발생합니다. 왜 이것이 작동하지 않는지 알 수가 없습니다.Github API JSON PHP에서 아무 것도 반환하지 않는 호출

저는 PhpStorm을 통해 로컬 호스트에서 실행하고 있습니다.

코드 :

$url = urlencode("https://api.github.com/users/devinmatte/repos"); 
$json = file_get_contents($url); 
$obj = json_decode($json); 
echo $obj; 

오류 :

[Sat Mar 25 20:02:14 2017] PHP Warning: file_get_contents(https%3A%2F%2Fapi.github.com%2Fusers%2Fdevinmatte%2Frepos): failed to open stream: No such file or directory in /home/devin-matte/Documents/Git-Challenge/index.php on line 13 
+0

를, "자식 문제는"무엇인가? –

+0

Git-Challenge는 내가 작업하고있는 프로젝트의 이름입니다. 나는 Game-ify에 학생들을위한 git 저장소에 기여하려고 노력하고있다. –

답변

3

몇 가지 오류가 있습니다 :

  • 당신이 상황을
  • 를 만들 필요가 urlencode의 URL
  • 할 필요는 print_r, var_export 등 대신 echo의에 배열의 내용을 인쇄 할 필요가있다.

이 작업을 수행해야합니다 호기심에서

$url = "http://api.github.com/users/devinmatte/repos"; 
$opts = [ 
    'http' => [ 
     'method' => 'GET', 
     'header' => [ 
       'User-Agent: PHP' 
     ] 
    ] 
]; 

$json = file_get_contents($url, false, stream_context_create($opts)); 
$obj = json_decode($json); 
var_dump($obj); 
2

당신은 전체 URI를 인코딩 URL있다가, file_get_contents이 유효한 HTTP URI를 기대 :

$json = file_get_contents("https://api.github.com/users/devinmatte/repos"); 
$obj = json_decode($json); 
var_dump($obj); 
+0

이 코드를 실행하면 https://api.github.com/users/devinmatte/repos가 null인데도 불구하고'$ obj = NULL'이됩니다. null JSON 객체가 아닙니다. –

+0

이 코드는 저에게 유용합니다. 로그를 확인할 수 있습니까? –

+0

나는 이것이 나의 기록이다 : '[Sat Mar 25 20:30:15 2017] PHP 경고 : file_get_contents (https://api.github.com/users/devinmatte/repos) : 스트림을 열지 못했습니다 : HTTP 요청 실패한! HTTP/1.0 403 금지됨 /home/devin-matte/Documents/Git-Challenge/index.php 온라인 12' –