An excerpt from http://www.sitepoint.com/popular-users-per-language-region-silex-github/, 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 homestead.app which loads your php files from ~/Code.
sites:
- map: homestead.app
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:
sites:
- map: homestead.app
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:
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/', function(){
return "Hello world";
});
$app->run();
If you open up homestead.app:8000 in your browser now, you should see “Hello World” (provided homestead.app 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.
<?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);
});
$app->run();
If you go to http://homestead.app:8000, you should see parhamdoustdar’s details in json:
{
"login":"parhamdoustdar",
"id":352539,
"avatar_url":"https:\/\/avatars.githubusercontent.com\/u\/352539?v=3",
"gravatar_id":"","url":"https:\/\/api.github.com\/users\/parhamdoustdar",
"html_url":"https:\/\/github.com\/parhamdoustdar",
"followers_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/followers",
"following_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/following{\/other_user}",
"gists_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/gists{\/gist_id}",
"starred_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/starred{\/owner}{\/repo}",
"subscriptions_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/subscriptions",
"organizations_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/orgs",
"repos_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/repos",
"events_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/events{\/privacy}",
"received_events_url":"https:\/\/api.github.com\/users\/parhamdoustdar\/received_events",
"type":"User",
"site_admin":false,
"name":"Parham Doustdar",
"company":"TENA",
"blog":"http:\/\/www.parhamdoustdar.com",
"location":"Tehran",
"email":"parham90@gmail.com",
"hireable":true,
"bio":null,
"public_repos":9,
"public_gists":2,
"followers":3,
"following":0,
"created_at":"2010-08-03T07:56:17Z",
"updated_at":"2015-03-01T20:01:26Z"}
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.