2017 © Pedro Peláez
 

library github-api

image

mpscholten/github-api

  • Sunday, April 27, 2014
  • by mpscholten
  • Repository
  • 1 Watchers
  • 14 Stars
  • 2,688 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

github-api

Build Status Latest Stable Version License, (*1)

An easy to use github api client for PHP., (*2)

Requirements

You need php 5.4 or higher to use this library., (*3)

Features

  • very easy to use and ide-friendly
  • pure object oriented interface
  • automatically handled pagination
  • psr-2

Get started

Install via composer: composer require mpscholten/github-api v0.3, (*4)

{
    "require": {
        "mpscholten/github-api": "v0.3"
    }
}

Auth

OAuth

To use oauth just pass your oauth token to Github::create() like this., (*5)

<?php

use MPScholten\GitHubApi\Github;

$github = Github::create('oauth token');

No authentication

If you want to use the public api without any authentication you can do this by just calling Github::create without any arguments., (*6)

<?php

use MPScholten\GitHubApi\Github;

$github = Github::create();

User API

In case you are using oauth you can get the current logged-in user by calling, (*7)

$user = Github::create('oauth token')->getCurrentUser();

Otherwise you can get users by their github username., (*8)

$user = Github::create()->getUser('mpscholten');

With the user object you can now do, (*9)

$user->getEmail();
$user->getName();
$user->getUrl();
$user->getAvatarUrl();
// ...

// relations
$user->getRepositories(); // returns an array of Repositories owned by the user
$user->getOrganizations();

// list the users repositories
foreach ($user->getRepositories() as $repository) {
    echo $repository->getName();
}

// with the 'user:email' oauth scope
$user->getPrimaryEmail();
$user->getEmails();

foreach ($user->getEmails() as $email) {
    if ($email->isVerified()) {
        echo $email;
    }
}

Repository API

$repository = Github::create()->getRepository('mpscholten', 'github-api');
$repository->getName();
$repository->getCommits();
$repository->getBranches();

$repository->getOwner(); // returns a user object
$repository->getOwner()->getName(); // chaining 

// list the collaborators of the repo
foreach ($repository->getCollaborators() as $collaborators) {
    echo $collaborators->getName();
}

Organization API

foreach ($user->getOrganizations() as $org) {
    $org->getName(); // e.g. GitHub
    $org->getLocation(); // e.g. San Francisco
}

Search API

You can use the search api by calling $github->getSearch(), (*10)

// this is equals to https://github.com/search?q=language%3Aphp+&type=Repositories&ref=searchresults
foreach (Github::create()->getSearch()->findRepositories('language:php') as $repo) {
    $repo->getName();
    // ...
}

Release API

foreach ($repository->getReleases() as $release) {
    $release->getUrl(); // https://github.com/octocat/Hello-World/releases/v1.0.0
    $release->getUrl('zipball'); // https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0
    $release->getCreatedAt()->format('Y-m-d H:i:s');
}

Issue API

foreach ($repository->getIssues() as $issue) {
    $issue->getLabels()->count();

    $issue->getNumber(); // 2
    $issue->getAuthor()->getLogin(); // "mpscholten"
    $issue->getTitle(); // "Add Issue-API"
    $issue->getBody();

    $issue->isOpen();
    $issue->isClosed();
    $issue->getState();
}

Pagination

Don't worry about pagination, all paginated collections are using a custom Iterator so we can automatically load more results if you need them. So you can focus on what you really want to do., (*11)

Example This will print you all commits of the repository., (*12)

foreach ($repository->getCommits() as $commit) {
    echo $commit->getMessage() . "\n";
}

Caching

It's builtin! By default we will use in-memory caching but you might want to use file caching. Just pass your cache directory to Github::create(), like this, (*13)

<?php

use MPScholten\GitHubApi\Github;

$github = Github::create('oauth token', 'my-cache-dir/');

Testing

$ phpunit

Contributing

Feel free to send pull request!, (*14)

The Versions

27/04 2014

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marc Scholten

12/04 2014

v0.3.1

0.3.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marc Scholten

10/03 2014

v0.3

0.3.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marc Scholten

09/03 2014

v0.2.1

0.2.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marc Scholten

05/03 2014

v0.2

0.2.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marc Scholten

28/02 2014

v0.1

0.1.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marc Scholten