Skip to content

diversen/simple-php-github-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

Very simple github API for PHP using OAuth. 37 LOC for the API class. And a curl helper class with 84 LOC.

Brief explantion.

There is really only tree methods you can do. Let us see those tree calls first. (Further below is an complete example using the built-in server) for easy testing).

  1. Generate an access URL to github.com
$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. Callback from 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 call
// We have a access token and we can now call the api: 
$api = new githubapi();

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

// example
// $command = '/user', 
// $request = 'GET', 'POST' or 'PATCH' or 'DELETE' etc. Se API: 
// [https://developer.github.com/v3/](https://developer.github.com/v3/)
// $post = variables to POST array

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

Example

Example you can run right away using the built-in PHP-server.

Make a github app

Log into github.com

Register a new application at https://github.com/settings/developers

You will se something like this:

Create your app.

Clone the simple-php-github-api source

git clone https://github.com/diversen/simple-php-github-api

cd simple-php-github-api

Configuration

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

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

Edit config

Set config in example/config.php according to above settings and the screenshot above.

Run test-server with example:

php -S localhost:8080 -t example/

More github API info

For full listing of all API calls check:

http://developer.github.com/

I have not tested many calls - but you should be able to use all. E.g. POST, or PATCH, DELETE.

Let me hear if it does not work out for you.

About

a simple php github api

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages