PHP ActiveX Box: Create Folder

Back to Index

Creates a new folder. To create a folder in the root folder, use a parent folder id = "0".

Documentation: https://developer.box.com/reference#create-a-new-folder

CURL Command

curl https://api.box.com/2.0/folders \
-H "Authorization: Bearer BOX_ACCESS_TOKEN" \
-d '{"name":"Important Docs", "parent": {"id": "BOX_FOLDER_ID"}}' \
-X POST

PHP ActiveX Example

<?php

$rest = new COM("Chilkat_9_5_0.Rest");

//  URL: https://api.box.com/2.0/folders
$bTls = 1;
$port = 443;
$bAutoReconnect = 1;
$success = $rest->Connect('api.box.com',$port,$bTls,$bAutoReconnect);
if ($success != 1) {
    print 'ConnectFailReason: ' . $rest->ConnectFailReason . "\n";
    print $rest->LastErrorText . "\n";
    exit;
}

$json = new COM("Chilkat_9_5_0.JsonObject");
$json->UpdateString('name','Important Docs');
$json->UpdateString('parent.id','BOX_FOLDER_ID');

$rest->AddHeader('Authorization','Bearer BOX_ACCESS_TOKEN');

$sbRequestBody = new COM("Chilkat_9_5_0.StringBuilder");
$json->EmitSb($sbRequestBody);
$sbResponseBody = new COM("Chilkat_9_5_0.StringBuilder");
$success = $rest->FullRequestSb('POST','/2.0/folders',$sbRequestBody,$sbResponseBody);
if ($success != 1) {
    print $rest->LastErrorText . "\n";
    exit;
}

$jsonResponse = new COM("Chilkat_9_5_0.JsonObject");
$jsonResponse->LoadSb($sbResponseBody);

$type = $jsonResponse->stringOf('type');
$id = $jsonResponse->stringOf('id');
$sequence_id = $jsonResponse->stringOf('sequence_id');
$etag = $jsonResponse->stringOf('etag');
$name = $jsonResponse->stringOf('name');
$created_at = $jsonResponse->stringOf('created_at');
$modified_at = $jsonResponse->stringOf('modified_at');
$description = $jsonResponse->stringOf('description');
$size = $jsonResponse->IntOf('size');
$path_collectionTotal_count = $jsonResponse->IntOf('path_collection.total_count');
$created_byType = $jsonResponse->stringOf('created_by.type');
$created_byId = $jsonResponse->stringOf('created_by.id');
$created_byName = $jsonResponse->stringOf('created_by.name');
$created_byLogin = $jsonResponse->stringOf('created_by.login');
$modified_byType = $jsonResponse->stringOf('modified_by.type');
$modified_byId = $jsonResponse->stringOf('modified_by.id');
$modified_byName = $jsonResponse->stringOf('modified_by.name');
$modified_byLogin = $jsonResponse->stringOf('modified_by.login');
$trashed_at = $jsonResponse->IsNullOf('trashed_at');
$purged_at = $jsonResponse->IsNullOf('purged_at');
$content_created_at = $jsonResponse->stringOf('content_created_at');
$content_modified_at = $jsonResponse->stringOf('content_modified_at');
$owned_byType = $jsonResponse->stringOf('owned_by.type');
$owned_byId = $jsonResponse->stringOf('owned_by.id');
$owned_byName = $jsonResponse->stringOf('owned_by.name');
$owned_byLogin = $jsonResponse->stringOf('owned_by.login');
$shared_link = $jsonResponse->IsNullOf('shared_link');
$folder_upload_email = $jsonResponse->IsNullOf('folder_upload_email');
$parentType = $jsonResponse->stringOf('parent.type');
$parentId = $jsonResponse->stringOf('parent.id');
$parentSequence_id = $jsonResponse->IsNullOf('parent.sequence_id');
$parentEtag = $jsonResponse->IsNullOf('parent.etag');
$parentName = $jsonResponse->stringOf('parent.name');
$item_status = $jsonResponse->stringOf('item_status');
$item_collectionTotal_count = $jsonResponse->IntOf('item_collection.total_count');
$item_collectionOffset = $jsonResponse->IntOf('item_collection.offset');
$item_collectionLimit = $jsonResponse->IntOf('item_collection.limit');
$i = 0;
$count_i = $jsonResponse->SizeOfArray('path_collection.entries');
while ($i < $count_i) {
    $jsonResponse->I = $i;
    $type = $jsonResponse->stringOf('path_collection.entries[i].type');
    $id = $jsonResponse->stringOf('path_collection.entries[i].id');
    $sequence_id_bool = $jsonResponse->IsNullOf('path_collection.entries[i].sequence_id');
    $etag_bool = $jsonResponse->IsNullOf('path_collection.entries[i].etag');
    $name = $jsonResponse->stringOf('path_collection.entries[i].name');
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray('item_collection.entries');
while ($i < $count_i) {
    $jsonResponse->I = $i;
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray('item_collection.order');
while ($i < $count_i) {
    $jsonResponse->I = $i;
    $by = $jsonResponse->stringOf('item_collection.order[i].by');
    $direction = $jsonResponse->stringOf('item_collection.order[i].direction');
    $i = $i + 1;
}


?>

Sample JSON Response Body

{
  "type": "folder",
  "id": "47885473705",
  "sequence_id": "0",
  "etag": "0",
  "name": "Important Docs",
  "created_at": "2018-03-16T06:54:57-07:00",
  "modified_at": "2018-03-16T06:54:57-07:00",
  "description": "",
  "size": 0,
  "path_collection": {
    "total_count": 1,
    "entries": [
      {
        "type": "folder",
        "id": "0",
        "sequence_id": null,
        "etag": null,
        "name": "All Files"
      }
    ]
  },
  "created_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "modified_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "trashed_at": null,
  "purged_at": null,
  "content_created_at": "2018-03-16T06:54:57-07:00",
  "content_modified_at": "2018-03-16T06:54:57-07:00",
  "owned_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "shared_link": null,
  "folder_upload_email": null,
  "parent": {
    "type": "folder",
    "id": "0",
    "sequence_id": null,
    "etag": null,
    "name": "All Files"
  },
  "item_status": "active",
  "item_collection": {
    "total_count": 0,
    "entries": [
    ],
    "offset": 0,
    "limit": 100,
    "order": [
      {
        "by": "type",
        "direction": "ASC"
      },
      {
        "by": "name",
        "direction": "ASC"
      }
    ]
  }
}