Tutorial·PHPPHP

PHP quick start

Install the bimbase/bimworks Composer package, log in with an API token, list your tree, upload a model and query it, in one script.

The PHP SDK is generated from the OpenAPI description, so it offers exactly the operations of the OpenAPI as typed classes: one Api class per tag (AuthApi, FsApi, ModelApi, IFCApi, GISApi and the others) and one Model class per schema. PHP 8.1 or later, with the curl, json and mbstring extensions.

Install#

The package is bimbase/bimworks on the BIM.works Composer repository. Add the repository and the package:

Shell#
composer config repositories.bimworks composer https://composer.bim.works
composer require bimbase/bimworks

The repository needs the credentials your BIM.works contact gives you, stored in auth.json; PHP shows how. The classes live in the BIMworks namespace and load through Composer's autoloader.

Log in#

<?php
require __DIR__ . '/vendor/autoload.php';

use BIMworks\Api\AuthApi;
use BIMworks\Api\FsApi;
use BIMworks\Api\IFCApi;
use BIMworks\Api\ModelApi;
use BIMworks\Configuration;
use BIMworks\Model\ListNodesRequest;
use BIMworks\Model\LoginWithApiTokenRequest;
use BIMworks\Model\QueryRequest;
use BIMworks\ObjectSerializer;

// 1. Connect to BIM.works and log in with your API token
$client = (new Configuration())->setHost('https://bim.works/openapi');
$auth = (new AuthApi(config: $client))->loginWithApiToken(new LoginWithApiTokenRequest(['token' => "<your API token>"]));
$client->setAccessToken($auth->getBase64());

// 2. The request: the root of the token, the node it was created on; paths are relative to it
$request = ObjectSerializer::deserialize(<<<'JSON'
{
  "path": "/"
}
JSON, ListNodesRequest::class);

// 3. The call
$result = (new FsApi(config: $client))->listNodes($request);

// 4. The result
foreach ($result as $node) {
    echo ($node->getFolder() ? 'folder ' : 'model  ') . $node->getName() . PHP_EOL;
}

// 2. The request: a file uploaded into the root, as a new model or a new version of the model of that name
$request = ['file' => new SplFileObject('model.ifc'), 'filename' => 'model.ifc'];

// 3. The call
$result = (new ModelApi(config: $client))->uploadModel(...$request);

// 4. The result
$versionUuid = $result->getUuid();
echo 'Uploaded as version ' . $versionUuid . PHP_EOL;

// 2. The request: a query of the new version
$request = ObjectSerializer::deserialize(json_encode([
    'models' => [['type' => 'VersionModelIdentifier', 'uuid' => $versionUuid]],
    'query' => 'GET qid, attribute.Name WHERE type = IFCDOOR LIMIT 10',
]), QueryRequest::class);

// 3. The call
$result = (new IFCApi(config: $client))->query($request);

// 4. The result
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;

The script has the four steps of every PHP SDK example (reference/sdk/php): it connects once, then repeats the request, the call and the result for each of its three calls. A Configuration carries the host and, after the login, the auth token; every Api object is created with it. loginWithApiToken returns an AuthResult whose getBase64() is the token to send as bearer, valid for at most 90 days.

The host is your instance's address followed by /openapi; the generated default points at https://dev.bim.works/openapi, so always set it. Every call throws BIMworks\ApiException on a non-2xx response, with the HTTP code and the server's ErrorResponse in the message. The script has the same four steps as every example of the PHP SDK (reference/sdk/php#example-shape): it connects once and repeats the request, the call and the result for each call.

List the tree#

FsApi::getRootNodeUuid() is the node your token was created on and returns its UUID as a string; listNodes takes a ListNodesRequest with a path relative to that root (or a uuid) and returns an array of Node objects. The script makes the request from its JSON with ObjectSerializer::deserialize, as every example does. getName(), getFolder(), getPath() and getLastVersionUuid() are the getters you need; a folder has no last version.

Upload#

ModelApi::uploadModel takes the form fields as named arguments (the script spreads an array of them into the call): the file, an SplFileObject, its filename, and optionally the node_uuid of the folder or model node to upload into (the root of your token without it). It sends the file as multipart/form-data and returns the new ModelVersion when the server has processed it. Pass filename: the server names the model and reads its file type from it. addModelWithUrl is the alternative for files the server can fetch itself.

Query#

IFCApi::query() takes a QueryRequest with the models (a VersionModelIdentifier per version UUID, with its type) and the BQL text, and returns the rows as an array of stdClass objects, one per row with every feature under its path; the script prints them as JSON. An empty models array validates the query without running it. The BQL course teaches the language.

Where to go from here#

The other operations follow the same request-object pattern: AuthApi::createVersionsToken for tokens that only see the versions you name (Tokens and delegation), IFCApi::check for IDS and BCL checks, ModelApi::download and VersionsApi::version for the original file, IFCApi::commitTransaction to write changes back. The generated PHP reference lists every class; the OpenAPI reference describes the operations themselves.