Giter Site home page Giter Site logo

simple-php-github-api's Introduction

About

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

Install simple-php-github-api

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

Or if you have placed composer.phar in your path as composer

composer require diversen/simple-php-github-api

Brief explantion.

There is really only tree methods you can do. Let us see those three methods 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

For full listing see: 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);
}

Full 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 see something like this:

My settings

Create your app.

Enter base_dir of the simple-php-github-api:

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

Configuration

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:

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

Scope:

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

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

Support

Create an issue, and Let me hear if it does not work out for you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.