PHP code example of krzysztofzylka / github

1. Go to this page and download the library: Download krzysztofzylka/github library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

krzysztofzylka / github example snippets


use krzysztofzylka\github\Github;
use krzysztofzylka\github\Auth\TokenAuth;

// Create authentication
$auth = new TokenAuth('your-personal-access-token');

// Initialize GitHub client
$github = new Github($auth);

// Get authenticated user
$user = $github->users()->me();

// Get repository information
$repo = $github->repositories()->get('owner', 'repository-name');

// List issues
$issues = $github->issues()->all('owner', 'repository-name');

use krzysztofzylka\github\Auth\TokenAuth;

$auth = new TokenAuth('your-personal-access-token');

use krzysztofzylka\github\Auth\OAuthAuth;

$auth = new OAuthAuth('your-oauth-token');

use krzysztofzylka\github\Auth\BasicAuth;

$auth = new BasicAuth('username', 'password');

// Get repository
$repo = $github->repositories()->get('owner', 'repo-name');

// Create repository
$newRepo = $github->repositories()->create([
    'name' => 'new-repository',
    'description' => 'Repository description',
    'private' => false
]);

// List user repositories
$repos = $github->repositories()->listUserRepos('username');

// List branches
$branches = $github->repositories()->listBranches('owner', 'repo-name');

// Create branch
$branch = $github->repositories()->createBranch('owner', 'repo-name', 'new-branch', 'commit-sha');

// List issues
$issues = $github->issues()->all('owner', 'repo-name', [
    'state' => 'open',
    'labels' => 'bug'
]);

// Create issue
$issue = $github->issues()->create('owner', 'repo-name', [
    'title' => 'Bug report',
    'body' => 'Issue description',
    'labels' => ['bug', 'help wanted']
]);

// Update issue
$updatedIssue = $github->issues()->update('owner', 'repo-name', 123, [
    'title' => 'Updated title'
]);

// Close issue
$github->issues()->close('owner', 'repo-name', 123);

// List pull requests
$prs = $github->pullRequests()->all('owner', 'repo-name', [
    'state' => 'open'
]);

// Create pull request
$pr = $github->pullRequests()->create('owner', 'repo-name', [
    'title' => 'Feature implementation',
    'head' => 'feature-branch',
    'base' => 'main',
    'body' => 'Pull request description'
]);

// Merge pull request
$github->pullRequests()->merge('owner', 'repo-name', 123, [
    'merge_method' => 'squash'
]);

// Get authenticated user
$user = $github->users()->me();

// Get user by username
$user = $github->users()->get('username');

// Update user profile
$updatedUser = $github->users()->update([
    'name' => 'New Name',
    'bio' => 'Updated bio'
]);

// List user organizations
$orgs = $github->users()->organizations('username');

// Check token scopes
$scopes = $github->authorization()->getScopes();

// Check if token has specific scope
if ($github->authorization()->hasScope('repo')) {
    // Can access private repositories
}

// Check if token can perform action
if ($github->authorization()->canPerformAction('create_repo')) {
    // Can create repositories
}

// Get rate limit information
$rateLimit = $github->authorization()->getRateLimit();

// Check if token is valid
if ($github->authorization()->isTokenValid()) {
    // Token is working
}

try {
    $repo = $github->repositories()->get('owner', 'non-existent-repo');
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
    echo "Code: " . $e->getCode();
}

// Get current rate limit information
$rateLimit = $github->getClient()->getRateLimit();

echo "Limit: " . $rateLimit['limit'];
echo "Remaining: " . $rateLimit['remaining'];
echo "Reset time: " . date('Y-m-d H:i:s', $rateLimit['reset']);

// Get all repositories (automatically paginated)
$allRepos = $github->repositories()->listUserRepos('username');

// Limit pagination to specific number of pages
$limitedRepos = $github->getClient()->paginate('/user/repos', [], 5);