diversen/simple-php-github-api

1.1.3 2016-02-16 10:24 UTC

This package is auto-updated.

Last update: 2024-09-20 08:15:43 UTC


README

一个非常简单的使用OAuth的PHP GitHub API。API类的代码为37行。还有一个curl辅助类,代码为84行。

安装 simple-php-github-api

php composer.phar require diversen/simple-php-github-api:

或者如果您已将composer.phar放置在 your path 并将其命名为composer

composer require diversen/simple-php-github-api

简要说明。

实际上只有三种方法可以进行操作。让我们先看看这三种方法。(下面是一个使用内置服务器的完整示例,方便测试)。

  1. 生成指向github.com的访问URL
$access_config = array (
    'redirect_uri' => GITHUB_CALLBACK_URL,
    'client_id' => GITHUB_ID,
    'state' =>  md5(uniqid()),
    'scope' => 'user' 
);

$api = new githubapi();
$url = $api->getAccessUrl($access_config);
echo "<a href=\"$url\">Github Login</a>";
  1. 来自github.com的回调
$access_config = array (
    'redirect_uri' => GITHUB_CALLBACK_URL,
    'client_id' => GITHUB_ID,
    'client_secret' => GITHUB_SECRET
);

$api = new githubapi();
$res = $api->setAccessToken($access_config);

if ($res) {
    // OK
    This is where we will call the api
    header("Location: /api_call.php");
} else {
    // Not OK. echo errors
    echo "Could not get access token. Errors: <br />";
    print_r($api->errors);
}
  1. API调用

完整列表请见: https://developer.github.com/v3/

// We have a access token and we can now call the api: 
$api = new githubapi();

// Simple call - API get current users credentials
// This can also be done without scope

// example
// $command = '/user', 
// $request = 'GET', 'POST' or 'PATCH' or 'DELETE' etc. Se API: 
// $post = variables to POST array

$command = "/user";
$res = $api->apiCall($command, $request = null, $post = null);
if (!$res) {
    print_r($api->errors); 
    die;
} else {
    print_r($res);
}

完整示例

您可以直接运行的示例,使用内置的PHP服务器。

创建GitHub应用

登录到 github.com

https://github.com/settings/developers 注册新的应用程序

您将看到如下内容

My settings

创建您的应用。

输入simple-php-github-api的base_dir

cd vendor/diversen/simple-php-github-api

配置

cp example/config.php-dist example/config.php

编辑配置

根据上述设置和截图在example/config.php中设置配置。

使用示例运行测试服务器

php -S localhost:8080 -t example/

更多GitHub API信息

检查完整API调用列表:

https://developer.github.com/v3/

作用域

https://developer.github.com/v3/oauth/#scopes

我没有测试很多调用,但您应该能够使用所有调用。例如,POST、PATCH或DELETE。

支持

创建一个问题,并让我知道它是否对您不起作用。