Created
December 13, 2014 06:28
-
-
Save faryne/4373b04f32615508e50b to your computer and use it in GitHub Desktop.
GitLab Code Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// API的基本路徑 | |
$url = 'http://example.com/api/v3'; | |
// 因為要列出projects,所以加上/projects | |
$url .= '/projects'; | |
// GitLab認證的方式有兩種,一種是把private_token放在query_string內,另一種是放在http header內 | |
// 可以根據自己需求來決定怎麼用。 | |
// 不過這邊我採用放在query_string的方法來作 | |
$url .= '?private_token=[my private token]'; | |
// 如果你擁有/管理的project很多,你可以在query_string中指定page=N(N>=1), | |
// 就可以指定要載入第N頁的資料 | |
// 如果不指定的話預設載入第一頁 | |
// 產生一個curl物件 | |
$ch = curl_init($url); | |
// 因為列出project的API是用HTTP的GET,所以CURLOPT就只要設定一個CURLOPT_RETURNTRANSFER就好 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// 設定結束,直接呼叫curl_exec | |
$result = curl_exec($ch); | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
// 沒意外的話你應該會看到一串json內容,接下來用json_decode解開就可以了 | |
$json = json_decode($result, true); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment