A complete PHP7 wrapper for GitHub API v3


PHP

GitHub is a popular web-based Git repository hosting service, which offers all of the distributed revision control and source code management functionality of Git as well as adding its own features.

Since may 2013, GitHub announced Octokit, a new lineup of GitHub-maintained client libraries for the GitHub API.
This means Octokit provide officially 3 libraries written in Ruby, Objective-C and .NET.

However, third-party libraries are available in other languages and particularly in PHP. At this time you can find at least 9 libraries to interact with GitHub API in PHP but none of this libraries provide a full support for all GitHub API features.

This library as been developed to help you to use all the features of GitHub API v3 easily as possible. This library, like the GitHub API is separate in 2 distinct parts:

  1. The Client part who will be used to get informations about a repository, a user, a commit, …
  2. The Server part, called WebHooks that can be used to get events from GitHub API

Features

This library provide the next features:

  • Supports all methods (Activity, Enterprise, Gists, Git Data, Issues, Miscellaneous, Organizations, Pull Requests, Repositories, Search, Users)
  • Supports Basic, OAuth and OAuth2 authentications.
  • Able to create a WebHooks server.

Minimum requirements

To run this library in your web server, you’ll need the most recent PHP version of the branch 7.x. for some security and dependencies reasons.

This library is only compatible with PHP7 or above. So please upgrade your PHP version if you have a lower version installed.

You will also need the cURL extension enabled in your web server.

Dependencies

This library use some dependencies to work with.
Here are the dependencies this library need to be installed with:

Installation

To get the latest version of this GitHub API library simply use composer, by executing the next command:

$ composer require flexyproject/githubapi

Client

<?php

// This file is generated by Composer
require 'vendor/autoload.php';

use FlexyProject\GitHub\{
    Client,
    Receiver\Miscellaneous
};

// Create a Client object
$client = new Client();

// Miscellaneous
$miscellaneous = $client->getReceiver(Client::MISCELLANEOUS);

// Get Emojis class object
$emojis = $miscellaneous->getReceiver(Miscellaneous::EMOJIS);

// Lists all the emojis available to use on GitHub.
$emojis->get();

Full documentation of the GitHub Client.

WebHook

Webhooks allow you to build or set up integrations which subscribe to certain events on GitHub.com.
Complete documentation is available at https://developer.github.com/webhooks/.

For using a WebHook, you need an instance of FlexyProject\GitHub\WebHook class like:

<?php

// This file is generated by Composer
require 'vendor/autoload.php';

use FlexyProject\GitHub\WebHook;

// Creating WebHook instance
$webhook = new WebHook();

// Get a Payload Event object
$payload = $webhook->getEvent(WebHook::PAYLOAD);

// Specify secret if configured in your webhook to secure your payload
$payload->setSecret('your-secret');

// Parse raw data
$payload->parse();

// Get decoded raw data from `application/json` or `application/x-www-form-urlencoded` content type
$payload->getData();

Full documentation of the GitHub WebHook.

Authentication

This library supports all authentications provided by the GitHub API v3.
You can use the Basic Authentication, OAuth Token or the OAuth2 Key/Secret pair authentication for the Client and WebHook classes, like described here:

Basic Authentication

// Username only
$client->setHttpAuth('username');

// Username & password
$client->setHttpAuth('username', 'password');

OAuth Token

Go to Authorized applications to Generate new token, then you will be able to provide your ‘token’.

// sent in userpwd
$client->setToken('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

// sent in a header
$client->setToken('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', \FlexyProject\Github\Client::OAUTH2_HEADER_AUTH);

// sent as a parameter
$client->setToken('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', \FlexyProject\Github\Client::OAUTH2_PARAMETERS_AUTH);

OAuth2 Key/Secret pair

Go to Authorized applications to Register new application, then you will be able to provide yours client_id and client_secret.

$client->setClientId(''); // Set client_id
$client->setClientSecret(''); // Set client_secret

You can find the full documentation of this library in the wiki section of the GitHub repository.