PHP code example of schmunk42 / php-bcx-client

1. Go to this page and download the library: Download schmunk42/php-bcx-client 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/ */

    

schmunk42 / php-bcx-client example snippets




use Schmunk42\BasecampApi\Authentication\BasicAuthentication;
use Schmunk42\BasecampApi\Client\BasecampClient;

// Your Basecamp credentials
$accountId = '999999999'; // Your account ID
$username = '[email protected]'; // Your Basecamp email
$password = 'your-password'; // Your Basecamp password

// Create authenticated client
$auth = new BasicAuthentication($username, $password);
$client = new BasecampClient($accountId, $auth);

// Get all projects
$projects = $client->projects()->all();

// Get current user
$me = $client->people()->me();
echo "Logged in as: {$me['name']}\n";



use Schmunk42\BasecampApi\Authentication\OAuth2Authentication;
use Schmunk42\BasecampApi\Client\BasecampClient;

// OAuth 2.0 access token (obtained through OAuth flow)
$accessToken = 'BAhbByIBsHsidmVyc2lvbiI6MSwidXNlcl9pZCI...';
$accountId = '999999999';

// Create authenticated client
$auth = new OAuth2Authentication($accessToken);
$client = new BasecampClient($accountId, $auth);

// Use the API
$projects = $client->projects()->all();

// List all projects
$projects = $client->projects()->all();

// Get archived projects
$archived = $client->projects()->archived();

// Create a new project
$project = $client->projects()->create([
    'name' => 'New Project',
    'description' => 'Project description',
]);

// Archive a project
$client->projects()->delete(123456);

// Get all todolists in a project
$todolists = $client->todolists()->all($projectId);

// Get todos in a todolist
$todos = $client->todos()->all($projectId, $todolistId);

// Get all todos across all todolists in a project
$allTodos = $client->todos()->allInProject($projectId);

// Filter todos by due date
$upcomingTodos = $client->todos()->allInProject($projectId, '2025-01-01');

// Get completed/remaining/trashed todos
$completed = $client->todos()->getCompleted($projectId, $todolistId);
$remaining = $client->todos()->getRemaining($projectId, $todolistId);
$trashed = $client->todos()->getTrashed($projectId, $todolistId);

// Project-level queries
$allCompleted = $client->todos()->getAllCompletedInProject($projectId);
$allRemaining = $client->todos()->getAllRemainingInProject($projectId);

// Create a new todo
$todo = $client->todos()->create($projectId, $todolistId, [
    'content' => 'Task description',
    'due_at' => '2025-12-31',
]);

// Mark todo as complete
$client->todos()->complete($projectId, $todoId);

// Global todolist queries
$activeLists = $client->todolists()->allGlobal();
$completedLists = $client->todolists()->getCompletedGlobal();
$trashedLists = $client->todolists()->getTrashedGlobal();

// Get todolist with many items (1000+) - excludes todos for performance
$largeTodolist = $client->todolists()->get($projectId, $todolistId, true);

// Get current user
$me = $client->people()->me();

// Get all people
$people = $client->people()->all();

// Get specific person
$person = $client->people()->get($personId);

// Get person's assigned todos across all projects
$assignedTodos = $client->people()->getAssignedTodos($personId);

// Filter assigned todos by due date
$upcomingTasks = $client->people()->getAssignedTodos($personId, '2025-01-01');

// Get person's activity events
$events = $client->people()->getEvents($personId);

// Get all projects accessible to a person
$projects = $client->people()->getProjects($personId);

// Get trashed (deleted) users (admin only)
$trashedUsers = $client->people()->getTrashed();

// Create a message
$message = $client->messages()->create($projectId, [
    'subject' => 'Project Update',
    'content' => 'Here is the latest update...',
]);

// Add a comment to a message
$comment = $client->comments()->create(
    $projectId,
    'Message',
    $messageId,
    ['content' => 'Great update!']
);

// Upload a file (two-step process)
$fileContent = file_get_contents('/path/to/file.pdf');
$upload = $client->uploads()->create($fileContent, 'application/pdf');
$token = $upload['token'];

// Attach to a message
$message = $client->messages()->create($projectId, [
    'subject' => 'Document attached',
    'content' => 'See attached file',
    'attachments' => [
        ['token' => $token, 'name' => 'document.pdf']
    ],
]);

$auth = new BasicAuthentication('[email protected]', 'password');
$client = new BasecampClient('999999999', $auth); // Try any number first
$me = $client->people()->me(); // Will show your accounts

use Schmunk42\BasecampApi\Exception\AuthenticationException;
use Schmunk42\BasecampApi\Exception\RequestException;

try {
    $projects = $client->projects()->all();
} catch (AuthenticationException $e) {
    // Handle authentication errors (401)
    echo "Auth failed: " . $e->getMessage();
} catch (RequestException $e) {
    // Handle other API errors (400, 404, 500, etc.)
    echo "API error: " . $e->getStatusCode();
    echo "Response: " . $e->getResponseBody();
}

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('basecamp');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));

$client = new BasecampClient($accountId, $auth, null, $logger);

// All API requests will be logged
$projects = $client->projects()->all();
bash
composer