PHP code example of oneduo / github-sdk-php

1. Go to this page and download the library: Download oneduo/github-sdk-php 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/ */

    

oneduo / github-sdk-php example snippets


use Oneduo\GitHubSdk\GitHubConnector;

$github = new GitHubConnector();

$response = $github->repos()->get('octocat', 'hello-world');
$repository = $response->json();

$response = $github->users()->list();
$users = $response->json();

$response = $github->users()->getByUsername('octocat');
$user = $response->json();

use Saloon\Http\Auth\TokenAuthenticator;

$github = new GitHubConnector();
$github->authenticate(new TokenAuthenticator('your-github-token'));

// Now you can access authenticated endpoints
$response = $github->users()->getAuthenticated();
$user = $response->json();

use Saloon\Http\Auth\BasicAuthenticator;

$github = new GitHubConnector();
$github->authenticate(new BasicAuthenticator('your-username', 'your-password'));

// Now you can access authenticated endpoints
$response = $github->users()->getAuthenticated();
$user = $response->json();

use Oneduo\GitHubSdk\Authenticators\AppAuthenticator;

// Using a private key file path
$github = new GitHubConnector();
$github->authenticate(new AppAuthenticator(
    appId: 'your-app-id',
    key: '/path/to/your/private-key.pem'
));

// Or using key content directly  
$github = new GitHubConnector();
$github->authenticate(new AppAuthenticator(
    appId: 'your-app-id',
    key: '-----BEGIN RSA PRIVATE KEY-----...'
));

// With passphrase if your key is encrypted
$github = new GitHubConnector();
$github->authenticate(new AppAuthenticator(
    appId: 'your-app-id',
    key: '/path/to/your/private-key.pem',
    passphrase: 'your-passphrase'
));

// Using an existing OpenSSLAsymmetricKey resource (PHP 8.0+)
$keyResource = openssl_pkey_get_private(file_get_contents('/path/to/key.pem'));
$github = new GitHubConnector();
$github->authenticate(new AppAuthenticator(
    appId: 'your-app-id',
    key: $keyResource
));

// Now you can access GitHub App endpoints
$response = $github->apps()->getAuthenticated();
$app = $response->json();

$github->authenticate(new TokenAuthenticator('your-github-token'));
$response = $github->repos()->listForOrg('oneduo');
$repos = $response->json();

$github->authenticate(new TokenAuthenticator('your-github-token'));
$response = $github->repos()->createForAuthenticatedUser([
    'name' => 'my-new-repo',
    'private' => true,
]);
$newRepo = $response->json();

use Saloon\Http\Request;
use Saloon\Enums\Method;

class MyCustomRequest extends Request {
    protected Method $method = Method::GET;
    
    public function resolveEndpoint(): string {
        return '/rate_limit';
    }
}

$response = $github->send(new MyCustomRequest());
$data = $response->json();
bash
composer