mpscholten / github-api
v0.3.1
2014-04-12 12:53 UTC
Requires
- php: >=5.4.0
- doctrine/cache: v1.3.0
- guzzle/guzzle: ~3.7
Requires (Dev)
- phpunit/phpunit: 3.7.*
- sami/sami: 1.4.*@dev
- squizlabs/php_codesniffer: 1.*
README
一个易于使用的PHP GitHub API客户端。
要求
您需要PHP 5.4或更高版本才能使用此库。
功能
- 非常易于使用且IDE友好
- 纯面向对象接口
- 自动处理分页
- psr-2
开始使用
通过composer安装: composer require mpscholten/github-api v0.3
{ "require": { "mpscholten/github-api": "v0.3" } }
认证
OAuth
要使用OAuth,只需将您的OAuth令牌传递给Github::create()
,如下所示。
<?php use MPScholten\GitHubApi\Github; $github = Github::create('oauth token');
无认证
如果您想在不进行任何认证的情况下使用公共API,您只需调用不带任何参数的Github::create
即可。
<?php use MPScholten\GitHubApi\Github; $github = Github::create();
用户API
如果您正在使用OAuth,您可以通过调用
$user = Github::create('oauth token')->getCurrentUser();
否则您可以通过GitHub用户名获取用户。
$user = Github::create()->getUser('mpscholten');
有了用户对象,您现在可以这样做
$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; } }
仓库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(); }
组织API
foreach ($user->getOrganizations() as $org) { $org->getName(); // e.g. GitHub $org->getLocation(); // e.g. San Francisco }
搜索API
您可以通过调用 $github->getSearch()
使用搜索API
// 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(); // ... }
发布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'); }
问题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(); }
分页
无需担心分页,所有分页集合都使用自定义的Iterator
,因此如果需要,我们可以自动加载更多结果。因此,您可以专注于您真正想要做的事情。
示例 这将打印出仓库的所有提交。
foreach ($repository->getCommits() as $commit) { echo $commit->getMessage() . "\n"; }
缓存
它是内置的!默认情况下,我们将使用内存缓存,但您可能想使用文件缓存。只需将您的缓存目录传递给Github::create()
,如下所示
<?php use MPScholten\GitHubApi\Github; $github = Github::create('oauth token', 'my-cache-dir/');
测试
$ phpunit
贡献
欢迎发送pull request!