Article: Popular Users per Language and Region with Silex and Github

An excerpt from, by Parham Doustdar

The Github API V3 is a very powerful set of API endpoints that you can use to retrieve useful information about your repositories, activities, and events. Not only that, it allows you to access public information on others. The fact that it has libraries in many languages and platforms is also a big plus; it allows you to get down to writing code very fast.

The data you can get through the GitHub API V3 is mostly what you can see on the GitHub web interface, except you can use the data in your application to come to interesting conclusions. That is what we are going to do in this article.

Younes Rafie has written an article titled How to Use Github’s API with PHP, in which he introduces a lot of what we are talking about here, although in a different fashion. If you haven’t read that article, though, do not fear! We have you covered.

The Concept

As Brandon Savage notes in his article, The definitive guide to contributing to open source:

There are millions of PHP developers, but only a (large) handful of contributors, authors and maintainers of open source software. Packagist reports a little under 50,000 packages in the PHP ecosystem. This is a huge number of open source packages, but is a small number compared to the PHP developers in the world.

Now, what if there was a web service in which we could specify a location and a language (e.g. PHP) and get a sorted list of contributors to open-source? It would certainly make Brandon very happy.

In our web service, we will have one endpoint (i.e. action, URL) that shows the most popular ten creators. The most popular creators are the people in a location who have the largest number of stargazers. For this, we will retrieve each person’s repositories and add up the number of times each one has been starred.

The GitHub Awards project does something similar, although in a much more complicated manner. It uses tools such as the GitHub Archive, which are outside of the scope of this article. Younes Rafie covered that approach somewhat, too, in this post. However, what we’ll do here is simply sort users by their number of followers, since that is a good indication of popularity as well.

Gearing Up

Setting Up the Development Machine

The easiest way to get started is to use either Laravel Homestead, or our very own Homestead Improved. Regardless of which one you use, if you open your homestead.yaml configuration file, you will see the default configuration, which is to have a site called which loads your php files from ~/Code.

    - map:
      to: /home/vagrant/Code/Laravel/public

Getting Silex and Setting Up Homestead.yaml

Being a micro-framework, Silex doesn’t have many rules or conventions. However, it helps to read this article to absorb some basics. If you don’t feel like switching between articles, we’ll link to relevant sections of the Silex documentation throughout this tutorial.

As you might know, Silex puts its index.php file in the web directory as per Symfony conventions. So, your webroot will actually be ~/Code/silex/web, rather than ~/Code/Laravel/public. Let’s go into the sites section of homestead.yaml and fix that:

    - map:
      to: /home/vagrant/Code/silex/web

Then, punch in vagrant up to boot up the VM and once it’s done, SSH into the VM with vagrant ssh. Inside, we do the following to initiate a new working Silex instance:

cd Code
composer create-project silex/silex

Once this procedure is complete (might take a while) create a subfolder in the silex folder called web and inside it a file index.php with the contents:

require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/', function(){
    return "Hello world";

If you open up in your browser now, you should see “Hello World” (provided is in your hosts file, as per instructions).

Congratulations! You are now ready to roll!

Getting Started With GitHub API V3

We’re going to use KNPLabs’ PHP GitHub API library to retrieve data from GitHub through its GitHub API V3. Let’s add it to our composer.json file by executing the command:

composer require knplabs/github-api ~1.4.

Let’s turn on the debug mode by setting $app[‘debug’] to true, and make a request to the GitHub API V3 to give us the details of a user named parhamdoustdar. Update index.php.

require_once __DIR__.'/../vendor/autoload.php';
use Github\Client;
use Silex\Application;
$app = new Application();
$app['debug'] = true;
$app->get('/', function (Application $app) {
    $client = new Client();
    $parham = $client->user()->show('parhamdoustdar');
    return $app->json($parham);

If you go to, you should see parhamdoustdar’s details in json:

    "name":"Parham Doustdar",

If you didn’t understand what exactly our controller is doing, don’t fret! We’ll go through it together.

On line 8, we’re turning on the debug mode as previously mentioned.

Inside the anonymous function that will be called whenever the root route (/) is accessed, we create an instance of Github\Client. Note that we’ve added an argument which is of type Silex\Application. As noted in the usage section of the Silex manual:

The current Application is automatically injected by Silex to the Closure thanks to the type hinting.

In the next line, we’re calling the user() method on the client object to get an object of type Github\Api\User, which is used to get information on users. We then simply call show() on this object, passing in the username we want to get information on.

The last line uses the json() helper method on the Silex\Application object to render our example json response.

Continue reading this article on SitePoint!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.