かまってちゃんに戻るよー。
昔phpで書かれたGitHubのAPIクライアントを探していて、これにstarを付けていたんだが
Using Laravel?
Laravel GitHub by Graham Campbell might interest you.
というのがあって、これは使ってみるしかないと思い早速見てみる。
どうやら12/12現在時点で5.0になったばかりのようだ。
README.mdにある通り、composerを使ってインストールしてみよう。
$ composer require graham-campbell/github
Using version ^5.0 for graham-campbell/github
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- knplabs/github-api 2.0.0 requires php-http/client-implementation ^1.0 -> no matching package found.
- graham-campbell/github v5.0.0 requires knplabs/github-api ^2.0 -> satisfiable by knplabs/github-api[2.0.0].
- Installation request for graham-campbell/github ^5.0 -> satisfiable by graham-campbell/github[v5.0.0].
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
Installation failed, reverting ./composer.json to its original content.
あらま。うまくインストールできない。ここで時間を取られるのも困るので、バージョンを下げてみよう。
$ composer require graham-campbell/github 4.4.2
./composer.json has been updated
Loading composer repositories with package information
- Installing guzzle/guzzle (v3.8.1)
Downloading: 100%
- Installing knplabs/github-api (1.7.1)
Downloading: 100%
- Installing graham-campbell/manager (v2.4.0)
Downloading: 100%
- Installing graham-campbell/github (v4.4.2)
Downloading: 100%
knplabs/github-api suggests installing knplabs/gaufrette (Needed for optional Gaufrette cache)
Package guzzle/guzzle is abandoned, you should avoid using it. Use guzzlehttp/guzzle instead.
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled class file has been removed.
おけ。いけたっぽい。
ちなみに更新されたcomposer.jsonは以下。
"type": "project",
"require": {
"php": ">=5.6.4",
- "laravel/framework": "5.3.*"
+ "laravel/framework": "5.3.*",
+ "graham-campbell/github": "4.4.2"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
README.mdにconfig/app.phpを修正しろとあるので
@@ -162,6 +162,7 @@ return [ Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, + GrahamCampbell\GitHub\GitHubServiceProvider::class, /* * Package Service Providers... @@ -225,6 +226,7 @@ return [ 'URL' => Illuminate\Support\Facades\URL::class, 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, + 'GitHub' => GrahamCampbell\GitHub\GitHubServiceProvider::class, ],
こんな感じで追加する。
っと、そういえばかまってちゃんアプリをGitHubに作ってなかったので作ろう。

まだ一つも作ってなかったので、ボタンポチーのcallbackにとりあえずlocalhost:8000を入れーのして作る。
するとclient IDとclient secretが取得できるようになる。
ここまでやって更に以下のコマンドをおもむろに叩く。
$ php artisan vendor:publish Copied Directory [/vendor/laravel/framework/src/Illuminate/Notifications/resources/views] To [/resources/views/vendor/notifications] Copied Directory [/vendor/laravel/framework/src/Illuminate/Pagination/resources/views] To [/resources/views/vendor/pagination] Copied File [/vendor/graham-campbell/github/config/github.php] To [/config/github.php] Publishing complete for tag []!
いらないものも作成されたがconfig/github.phpが作成された。 config/github.phpの雛形は
https://github.com/GrahamCampbell/Laravel-GitHub/blob/master/config/github.php
で、この clientId と clientSecret の値にさっき作ったアプリのclientIDとclient secretを登録する。
一応アプリケーションを使うのはalternative扱いらしい。Personal access tokens から個人用鍵を作って token を置き換えるのでも良い。
どれでもいいので自分の好きなものを使って登録する。
とりあえず叩いて結果が取れるかどうかを見たかったので、以下のようにしてRankingControllerを作り変える。
public function index()
{
- return response()->json($this->getDummy());
+ $client = new \Github\Client();
+ $repositories = $client->api('user')->repositories('kimikimi714');
+ return response()->json($repositories);
}
で、http://localhost:8000/api/rankings にアクセスすると

はい取れたー。
今日はここまで。