duangang4940
2017-03-26 00:01
浏览 66

Github API JSON调用在PHP中没有返回任何内容

I'm trying to get the data from this Api call with the Github Api. However whenever I try and get the data from it I get the below error. I cannot seem to figure out why this isn't working.

I'm running it on a localhost through PhpStorm.

Code:

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

Error:

[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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用Github Api从此Api调用中获取数据。 但是,每当我尝试从中获取数据时,我都会收到以下错误。 我似乎无法弄清楚为什么这不起作用。

我通过PhpStorm在本地主机上运行它。

代码:

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

错误:

  [Sat Mar 25  2017年20:02:14] PHP警告:file_get_contents(https%3A%2F%2Fapi.github.com%2Fusers%2Fdevinmatte%2Frepos):无法打开流:/ home / devin-matte / Documents中没有这样的文件或目录 第13行上的/Git-Challenge/index.php 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchan9354 2017-03-26 00:34
    已采纳

    There are few errors:

    • no need to urlencode the url
    • you need to create context
    • you need to print_r, var_export etc instead of echo to print content of an array.

    This should do the job:

    $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);
    
    已采纳该答案
    打赏 评论
  • dpdkqls6399 2017-03-26 00:19

    You are url encoding the whole URI, file_get_contents expect a valid http URI :

    $json = file_get_contents("https://api.github.com/users/devinmatte/repos");
    $obj = json_decode($json);
    var_dump($obj);
    
    打赏 评论

相关推荐 更多相似问题