检索 Github 用户使用的所有语言


Retrieve all languages used by a Github user

在我的脚本中,我有一个函数,可以从Github API检索JSON信息,https://api.github.com/users/octocat/repos。

我想有一个不同的函数来获取(在这种情况下)octocat使用的所有语言,然后计算他使用该语言的次数。

我在想这个:

        foreach($json['language'] as $RepoLanguage) 
        {
            echo $RepoLanguage;
        }

但这行不通,有什么建议/想法吗?

我认为主要原因是您没有指定此处指定的用户代理:https://developer.github.com/v3/#user-agent-required

您是否检查过$json?

的结果

这是一个工作示例。

<?php
    function get_content_from_github($url) {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
        curl_setopt($ch,CURLOPT_USERAGENT,'My User Agent');
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
    $json = json_decode(get_content_from_github('https://api.github.com/users/octocat/repos'), true);
    foreach($json as $repo) {
        $language = $repo['language'];
    }
?>