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
  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?