A simple Object Oriented wrapper for GitHub API, written with PHP5.
$github = new Github_Client();
$myRepos = $github->getRepoApi()->getUserRepos('ornicar');
Uses GitHub API v2. The object API is very similar to the RESTful API.
- Covers 100% of GitHub API with PHP methods
- Supports 3 authentication methods
- Follows PEAR conventions and coding standard: autoload friendly
- Light and fast thanks to lazy loading of API classes
- Flexible and extensible thanks to dependency injection
- Extensively tested and documented
- PHP 5.2 or 5.3.
- php curl, but it is possible to write another transport layer.
- PHPUnit to run tests.
The first step to use php-github-api is to register its autoloader:
require_once '/path/to/lib/Github/Autoloader.php';
Github_Autoloader::register();
Replace the /path/to/lib/
path with the path you used for php-github-api installation.
php-github-api follows the PEAR convention names for its classes, which means you can easily integrate php-github-api classes loading in your own autoloader.
$github = new Github_Client();
From this object, you can access to all GitHub apis, listed below.
Users | Issues | Commits | Objects | Repos | Pull Requests | Request any Route | Authentication & Security | Customize php-github-api | Run Test Suite
Searching users, getting user information and managing authenticated user account information. Wrap GitHub User API.
$users = $github->getUserApi()->search('ornicar');
Returns an array of users.
$user = $github->getUserApi()->show('ornicar');
Returns an array of information about the user.
Change user attributes: name, email, blog, company, location. Requires authentication.
$github->getUserApi()->update('ornicar', array('location' => 'France', 'blog' => 'http://diem-project.org/blog'));
Returns an array of information about the user.
$users = $github->getUserApi()->getFollowing('ornicar');
Returns an array of followed users.
$users = $github->getUserApi()->getFollowers('ornicar');
Returns an array of following users.
Make the authenticated user follow a user. Requires authentication.
$github->getUserApi()->follow('symfony');
Returns an array of followed users.
Make the authenticated user unfollow a user. Requires authentication.
$github->getUserApi()->unFollow('symfony');
Returns an array of followed users.
$users = $github->getUserApi()->getWatchedRepos('ornicar');
Returns an array of watched repos.
$emails = $github->getUserApi()->getEmails();
Returns an array of the authenticated user emails. Requires authentication.
$github->getUserApi()->addEmail('my-email@provider.org');
Returns an array of the authenticated user emails. Requires authentication.
$github->getUserApi()->removeEmail('my-email@provider.org');
Return an array of the authenticated user emails. Requires authentication.
Listing issues, searching, editing and closing your projects issues. Wrap GitHub Issue API.
$issues = $github->getIssueApi()->getList('ornicar', 'php-github-api', 'open');
Returns an array of issues.
$issues = $github->getIssueApi()->search('ornicar', 'php-github-api', 'closed', 'bug');
Returns an array of closed issues matching the "bug" term,.
$issue = $github->getIssueApi()->show('ornicar', 'php-github-api', 1);
Returns an array of information about the issue.
$github->getIssueApi()->open('ornicar', 'php-github-api', 'The issue title', 'The issue body');
Creates a new issue in the repo "php-github-api" of the user "ornicar". The issue is assigned to the authenticated user. Requires authentication. Returns an array of information about the issue.
$github->getIssueApi()->close('ornicar', 'php-github-api', 4);
Closes the fourth issue of the repo "php-github-api" of the user "ornicar". Requires authentication. Returns an array of information about the issue.
$github->getIssueApi()->reOpen('ornicar', 'php-github-api', 4);
Reopens the fourth issue of the repo "php-github-api" of the user "ornicar". Requires authentication. Returns an array of information about the issue.
$github->getIssueApi()->update('ornicar', 'php-github-api', 4, array('body' => 'The new issue body'));
Updates the fourth issue of the repo "php-github-api" of the user "ornicar". Requires authentication. Available attributes are title and body. Returns an array of information about the issue.
$comments = $github->getIssueApi()->getComments('ornicar', 'php-github-api', 4);
List an issue comments by username, repo and issue number. Returns an array of issues.
$github->getIssueApi()->addComment('ornicar', 'php-github-api', 4, 'My new comment');
Add a comment to the issue by username, repo and issue number. The comment is assigned to the authenticated user. Requires authentication.
$labels = $github->getIssueApi()->getLabels('ornicar', 'php-github-api');
List all project labels by username and repo. Returns an array of project labels.
$github->getIssueApi()->addLabel('ornicar', 'php-github-api', 'label name', 4);
Add a label to the issue by username, repo, label name and issue number. Requires authentication. If the label is not yet in the system, it will be created. Returns an array of the issue labels.
$github->getIssueApi()->removeLabel('ornicar', 'php-github-api', 'label name', 4);
Remove a label from the issue by username, repo, label name and issue number. Requires authentication. Returns an array of the issue labels.
$github->getIssueApi()->searchLabel('ornicar', 'php-github-api', 'label name')
Returns an array of issues matching the given label.
Getting information on specific commits, the diffs they introduce, the files they've changed. Wrap GitHub Commit API.
$commits = $github->getCommitApi()->getBranchCommits('ornicar', 'php-github-api', 'master');
Returns an array of commits.
$commits = $github->getCommitApi()->getFileCommits('ornicar', 'php-github-api', 'master', 'README');
Returns an array of commits.
$commit = $github->getCommitApi()->getCommit('ornicar', 'php-github-api', '726eac09a3b44411bd86');
Returns a single commit.
Getting full versions of specific files and trees in your Git repositories. Wrap GitHub objects API.
$tree = $github->getObjectApi()->showTree('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b');
Returns an array containing a tree of the repository.
$blobs = $github->getObjectApi()->listBlobs('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b');
Returns an array containing the tree blobs.
$blob = $github->getObjectApi()->showBlob('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b', 'CHANGELOG');
Returns array of blob informations.
$rawText = $github->getObjectApi()->getRawData('ornicar', 'php-github-api', 'bd25d1e4ea7eab84b856131e470edbc21b6cd66b');
The last parameter can be either a blob SHA1, a tree SHA1 or a commit SHA1. Returns the raw text content of the object.
Searching repositories, getting repository information and managing repository information for authenticated users. Wrap GitHub Repo API. All methods are described on that page.
$repos = $github->getRepoApi()->search('symfony');
Returns a list of repositories.
You can filter the results by language. It takes the same values as the language drop down on http://github.com/search.
$repos = $github->getRepoApi()->search('chess', 'php');
You can specify the page number:
$repos = $github->getRepoApi()->search('chess' , 'php', 2);
$repo = $github->getRepoApi()->show('ornicar', 'php-github-api')
Returns an array of information about the specified repository.
$repos = $github->getRepoApi()->getUserRepos('ornicar');
Returns a list of repositories.
$repos = $github->getRepoApi()->getPushableRepos();
Returns a list of repositories.
$repo = $github->getRepoApi()->create('my-new-repo', 'This is the description of a repo', 'http://my-repo-homepage.org', true);
Creates and returns a public repository named my-new-repo.
$repo = $github->getRepoApi()->setRepoInfo('username', 'my-new-repo', array('description' => 'some new description'));
The value array also accepts the parameters
- description
- homepage
- has_wiki
- has_issues
- has_downloads
Updates and returns the repository named 'my-new-repo' that is owned by 'username'.
$token = $github->getRepoApi()->delete('my-new-repo'); // Get the deletion token
$github->getRepoApi()->delete('my-new-repo', $token); // Confirm repository deletion
Deletes the my-new-repo repository.
$github->getRepoApi()->setPublic('reponame');
$github->getRepoApi()->setPrivate('reponame');
Makes the 'reponame' repository public or private and returns the repository.
$keys = $github->getRepoApi()->getDeployKeys('reponame');
Returns a list of the deploy keys for the 'reponame' repository.
$keys = $github->getRepoApi()->addDeployKey('reponame', 'key title', $key);
Adds a key with title 'key title' to the 'reponame' repository and returns a list of the deploy keys for the repository.
$keys = $github->getRepoApi()->removeDeployKey('reponame', 12345);
Removes the key with id 12345 from the 'reponame' repository and returns a list of the deploy keys for the repository.
$collaborators = $github->getRepoApi()->getRepoCollaborators('username', 'reponame');
Returns a list of the collaborators for the 'reponame' repository.
$collaborators = $github->getRepoApi->addCollaborator('reponame', 'username');
Adds the 'username' user as collaborator to the 'reponame' repository.
$collaborators = $github->getRepoApi->removeCollaborator('reponame', 'username');
Remove the 'username' collaborator from the 'reponame' repository.
$repository = $github->getRepoApi->watch('ornicar', 'php-github-api');
$repository = $github->getRepoApi->unwatch('ornicar', 'php-github-api');
Watches or unwatches the 'php-github-api' repository owned by 'ornicar' and returns the repository.
$repository = $github->getRepoApi->fork('ornicar', 'php-github-api');
Creates a fork of the 'php-github-api' owned by 'ornicar' and returns the newly created repository.
$tags = $github->getRepoApi()->getRepoTags('ornicar', 'php-github-api');
Returns a list of tags.
$tags = $github->getRepoApi()->getRepoBranches('ornicar', 'php-github-api');
Returns a list of branches.
$watchers = $github->getRepoApi()->getRepoWatchers('ornicar', 'php-github-api');
Returns list of the users watching the 'php-github-api' owned by 'ornicar'.
$network = $github->getRepoApi()->getRepoNetwork('ornicar', 'php-github-api');
Returns list of the forks of the 'php-github-api' owned by 'ornicar', including the original repository.
$contributors = $github->getRepoApi()->getRepoLanguages('ornicar', 'php-github-api');
Returns a list of languages.
$contributors = $github->getRepoApi()->getRepoContributors('ornicar', 'php-github-api');
Returns a list of contributors.
To include non GitHub users, add a third parameter to true:
$contributors = $github->getRepoApi()->getRepoContributors('ornicar', 'php-github-api', true);
Lets you list pull requests for a given repository, list one pull request in particular along with its discussion, and create a pull-request. Wraps GitHub Pull Request API, still tagged BETA. All methods are described there.
$openPullRequests = $github->getPullRequestApi()->listPullRequests( "ezsystems", "ezpublish", 'open' );
The last parameter of the listPullRequests method default to 'open'. The call above is equivalent to :
$openPullRequests = $github->getPullRequestApi()->listPullRequests( "ezsystems", "ezpublish" );
$openPullRequests
contains an array of open pull-requests for this repository.
$closedPullRequests = $github->getPullRequestApi()->listPullRequests( "ezsystems", "ezpublish", 'closed' );
$closedPullRequests
contains an array of closed pull-requests for this repository.
$pullRequest15 = $github->getPullRequestApi()->show( "ezsystems", "ezpublish", 15 );
The last parameter of this call, Pull request ID, can be either extracted from the results of the
listPullRequests
results ( 'number' key for a listed pull-request ), or added manually.
The $pullRequest15
array contains the same elements as every entry in the result of a listPullRequests
call, plus a "discussion" key, self-explanatory.
A pull request can either be created by supplying both the Title & Body, OR an Issue ID.
Details regarding the content of parameters 3 and 4 of the create
method are presented here : http://develop.github.com/p/pulls.html .
Requires authentication.
$title = "My nifty pull request";
$body = "This pull request contains a bunch of enhancements and bug-fixes, happily shared with you";
$github->getPullRequestApi()->create( "ezsystems", "ezpublish", "master", "testbranch", $title, $body );
This returns the details of the pull request.
Requires authentication. The issue ID is provided instead of title and body.
$issueId = 15;
$github->getPullRequestApi()->create( "ezsystems", "ezpublish", "master", "testbranch", null, null, $issueId );
This returns the details of the pull request.
The method you need does not exist yet? You can access any GitHub route by using the "get" and "post" methods. For example,
$repo = $github->get('repos/show/ornicar/php-github-api');
Returns an array describing the php-github-api repository.
See all GitHub API routes: http://develop.github.com/
Most GitHub services do not require authentication, but some do. For example the methods that allow you to change properties on Repositories and some others. Therefore this step is facultative.
GitHub provides some different ways of authentication. This API implementation implements three of them which are handled by one function:
$github->authenticate($username, $secret, $method);
$username is, of course, the username. $method is optional. The three allowed values are:
- Github_Client::AUTH_URL_TOKEN (default, if $method is omitted)
- Github_Client::AUTH_HTTP_TOKEN
- Github_Client::AUTH_HTTP_PASSWORD
The required value of $secret depends on the choosen $method. For the AUTH_*_TOKEN methods, you should provide the API token here. For the AUTH_HTTP_PASSWORD, you should provide the password of the account.
After executing the $github->authenticate($username, $secret, $method);
method using correct credentials, all further requests are done as the given user.
The Github_Client::AUTH_URL_TOKEN authentication method sends the username and API token in URL parameters. The Github_Client::AUTH_HTTP_* authentication methods send their values to GitHub using HTTP Basic Authentication. Github_Client::AUTH_URL_TOKEN used to be the only available authentication method. To prevent existing applications from changing their behavior in case of an API upgrade, this method is choosen as the default for this API implementation. Note however that GitHub describes this method as deprecated. In most case you should use the Github_Client::AUTH_HTTP_TOKEN instead.
If you want to stop new requests from being authenticated, you can use the deAuthenticate method.
$github->deAuthenticate();
The library is highly configurable and extensible thanks to dependency injection.
Wanna change, let's say, the http client User Agent?
$github->getHttpClient()->setOption('user_agent', 'My new User Agent');
See all available options in Github/HttpClient.php
php-github-api provides a curl-based implementation of a http client. If you want to use your own http client implementation, inject it to the Github_Client instance:
// create a custom http client
class MyHttpClient extends Github_HttpClient
{
public function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
// send the request and return the raw response
}
}
Your http client implementation may not extend Github_HttpClient, but only implement Github_HttpClientInterface.
You can now inject your http client through Github_Client constructor:
$github = new Github_Client(new MyHttpClient());
Or to an existing Github_Client instance:
$github->setHttpClient(new MyHttpClient());
If you want to use your own implementation of an API, inject it to the GitHubApi. For example, to replace the user API:
// create a custom User API
class MyGithubApiUser extends Github_Api_User
{
// overwrite things
}
$github->setApi('user', new MyGithubApiUser($github));
The code is unit tested. To run tests on your machine, from a CLI, run
phpunit
This library borrows ideas, code and tests from phptwitterbot.
- Thanks to noloh for his contribution on the Object API.
- Thanks to bshaffer for his contribution on the Repo API.
- Thanks to Rolf van de Krol for his countless contributions.
- Thanks to Nicolas Pastorino for his contribution on the Pull Request API.
Thanks to GitHub for the high quality API and documentation.