PHP code example of chancegarcia / php-box-sdk

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

    

chancegarcia / php-box-sdk example snippets


use Box\Service\BoxClientFactory;
use Box\Service\EnvConfigProvider;

$factory = new BoxClientFactory(new EnvConfigProvider());
$client  = $factory->createClient();

$authUrl = $client->buildAuthorizationUrl();
// Redirect user to $authUrl

$client->setAuthorizationCode($_GET['code']);
$token = $client->exchangeAuthorizationCodeForToken();

use Box\Service\BoxClientFactory;
use Box\Service\EnvConfigProvider;

$configProvider = new EnvConfigProvider();
$factory        = new BoxClientFactory($configProvider);
$client         = $factory->createClientForCurrentMode(); // JWT client when BOX_AUTH_MODE=jwt

use Box\Http\FileStream;

$client->setToken($token);

// Upload a local file
$result = $client->uploadFileToBox('/path/to/file.txt', '12345');

// Upload via stream (no local file needed)
$stream = FileStream::fromString('Hello World', 'hello.txt');
$result = $client->uploadFileToBox($stream, '0'); // '0' is the root folder ID

// Get a folder
$folder = $client->getFolder('12345');