PHP code example of chancegarcia / box-api-v2-sdk

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


use Box\Model\Client\Client;
$boxClient = new Client();
$boxClient->setClientId($boxClientId);
$boxClient->setClientSecret($boxClientSecret);

// see setup
$client->setAuthorizationCode($code);
$authUri = $client->buildAuthQuery();
$authUri .= '&state=' . $state;

// see setup
$state = $request->get('state');
$code = $request->get('code');
try {
    $token = $client->getAccessToken();
    // store the token somehow
} catch (\Box\Exception\Exception $e) {
    switch ($e->getError()) {
        case "invalid_grant":
            // do something to get another access code?
            $msg = 'invalid grant, try resending user to the auth uri (start of link process)';
            $logger->error($msg);
            break;
        case "invalid_request":
            // no break
        case "unauthorized_client":
            // no break
        case "invalid_client":
            // no break
        case "redirect_uri_mismatch":
            // no break
        case "insecure_redirect_uri":
            // no break
        case "invalid_redirect_uri":
            // no break
        default:
            break;
    }

    // bubble up exception
    throw $e;
}

use Box\Model\Client\Client;
use Box\Model\Connection\Token\Token;
$boxClient = new Client();
$boxClient->setClientId($boxClientId);
$boxClient->setClientSecret($boxClientSecret);

$oToken = new Token();
$oToken->setAccessToken($accessToken);
$oToken->setRefreshToken($refreshToken);

$client->setToken($oToken);
$token = $client->refreshToken();

use Box\Model\Client\Client;

$uploadResponse = $client->uploadFileToBox($uploadFilePath);