Esempio n. 1
0
<?php

// Step one: Creating a authorisation URL for github
// autoload
include_once "../../../autoload.php";
// very small boot file. Starts session. Defines constants.
include_once "boot.php";
// use the githubapi
use diversen\githubapi;
/**
 * This is the config we use when creating a url to github.com
 * 
 * Click the link, and we browser to the github site, 
 * and asks for the user to accept the scope of our application. 
 * 
 * 
 * Scope is set to 'user', but you could e.g. set it empty for the
 * lowest privileges 
 * 
 * We press the url, and we move to the github site where
 * the user will be able to accept that this app uses some
 * priviligies. If the user accepts, we return to callback.php 
 * 
 * Next /callback.php
 */
$access_config = array('redirect_uri' => GITHUB_CALLBACK_URL, 'client_id' => GITHUB_ID, 'state' => md5(uniqid()), 'scope' => GITHUB_SCOPE);
$api = new githubapi();
$url = $api->getAccessUrl($access_config);
echo "<a href=\"{$url}\">Github Login</a>";
Esempio n. 2
0
<?php

// Step tree: Call the API
include_once "../../../autoload.php";
// very small boot file. Starts session. Defines constants.
include_once "boot.php";
// Use the githubapi
use diversen\githubapi;
// 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
$command = "/user";
$res = $api->apiCall($command, 'GET', null);
if (!$res) {
    print_r($api->errors);
    die;
}
print_r($res);
// Or: more complex: first param is the command
// The next is the REQUEST Method
// 3. is an array with $post if we .eg. PATCH, or POST
// $res = $api->apiCall('/gists/4381068', 'PATCH', $content);
/*
* $content = array (
   'description' => 'mmmmmmmm....',
   'public' => 'true',
   'files' => array (
       'file7.txt' => array (
           'content' => 'New content from api'
        ),
<?php

// Step two: check if user has autorized our app
include_once "../../../autoload.php";
// very small boot file. Starts session. Defines constants.
include_once "boot.php";
// use the githubapi
use diversen\githubapi;
/*
 * We are back from github and the user has accepted our
 * request. We can now request an access token, 
 * The simple-php-github-api will just set this as a SESSION var found in
 * $_SESSION['access_token'], when we return from github
 * 
 * We check if everything is fine, and redirect to app_call.php
 * Here we will we make api calls using the $_SESSION['access_token']
 * 
 * Next step: 
 * 
 * /api_call.php
 * 
 */
$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) {
    header("Location: /api.php");
} else {
    echo "Could not get access token. Errors: <br />";
    print_r($api->errors);
}