user3640960
user3640960

Reputation: 99

Retrieve all languages used by a Github user

In my script I have a function that retrieves JSON information from the Github API, https://api.github.com/users/octocat/repos.

I want to have a different function to get all the languages used by (in this case) octocat and then count how many times he used the language.

I was thinking of this:

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

but that won't work, any suggestions/ideas?

Upvotes: 1

Views: 2301

Answers (1)

Manuel van Rijn
Manuel van Rijn

Reputation: 10305

I think the main reason is that you did not specify the User Agent as specified here: https://developer.github.com/v3/#user-agent-required

Did you check what result you have in the $json?

Here's a working example.

<?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'];
    }
?>

Upvotes: 2

Related Questions