GIt APIを使用する
〜API(WebAPI)を使用するのに、レンタルサーバーの都合でPHPを使用することになってしまったんでPHPでのGitAPIを使用します。
作成物は下のリンクです
https://zenryokuservice.com/mokuhyoAP.php
ツイッターが表示されています。
Gitにコミットした時に自動ツイートする処理を作成しました。
WebAPIはリクエストを送ればOK
PHPだろうが、PythonだろうがJavaだろうが。。。早い話が、WebAPIというのは以下の手順で使用します。
- WebAPIを提供してるURLにリクエストを送信。
- レスポンスを取得。
上記のような手順で使用します。。。なので使用する言語はリクエストが遅れればなんでもOKです。本当はJavaで実装したかった。。。がこれも今使っているレンタルサーバーでJavaが使えなかったので(もうちょっと調査すれば使えるかも?)後々に方法を考えることにします。
チュートリアル(コマンド実行)
参考にしたサイト
上のリンクにはGitAPIのチュートリアルが記載されています。(Githubのページなので英語です)
そして、そこにはコマンドでの使用方法が記載されています。
とりあえずはハローワールド的なコマンドを叩くような記載があったので、それを実行します。
curl https://api.github.com/users/ZenryokuService
これはcurl https://api.github.com/users/Gitユーザー名
でコマンドを叩いた時のものです。
追伸:パスワードなしでアクセスできる情報ですので問題ありません。
レスポンスの内容は以下のようなものです。
{ "login": "ZenryokuService", "id": 11029365, "node_id": "MDQ6VXNlcjExMDI5MzY1", "avatar_url": "https://avatars2.githubusercontent.com/u/11029365?v=4", "gravatar_id": "", "url": "https://api.github.com/users/ZenryokuService", "html_url": "https://github.com/ZenryokuService", "followers_url": "https://api.github.com/users/ZenryokuService/followers", "following_url": "https://api.github.com/users/ZenryokuService/following{/other_user}", "gists_url": "https://api.github.com/users/ZenryokuService/gists{/gist_id}", "starred_url": "https://api.github.com/users/ZenryokuService/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/ZenryokuService/subscriptions", "organizations_url": "https://api.github.com/users/ZenryokuService/orgs", "repos_url": "https://api.github.com/users/ZenryokuService/repos", "events_url": "https://api.github.com/users/ZenryokuService/events{/privacy}", "received_events_url": "https://api.github.com/users/ZenryokuService/received_events", "type": "User", "site_admin": false, "name": "Takunoji", "company": "ZenryokuService", "blog": "https://zenryokuservice.com/wp", "location": "Japan", "email": null, "hireable": null, "bio": "My favorite word is \"Rock'n roll must not be a day\"\r\n好きな言葉は「ロッケンロールは一日にしてならず」です。", "public_repos": 23, "public_gists": 1, "followers": 2, "following": 3, "created_at": "2015-02-16T12:53:02Z", "updated_at": "2019-05-19T08:53:20Z" }
見ての通りJSON形式でレスポンスを受け取りました。
PHPでのGit API
PHPの場合は。これをインストールしてやったほうが楽かも?
しかし、今作成しているプロジェクトサイトはツイッターAPIでコミットコメントを取得するので使わないかな?