1. Go to this page and download the library: Download nikba/directus-api 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/ */
nikba / directus-api example snippets
use Nikba\Directus\Directus;
/**
* @param string $url
* @param string $project
*/
$directus = new Directus('https://api.nikba.com/', 'projectname');
/**
* Temporary (JWT)
* These tokens are generated through the /auth/authenticate endpoint (below) and have a lifespan of 20 minutes.
* @param string $username
* @param string $password
*/
$directus->authenticate('username', '********');
/**
* Using Static token
* Each user can have one static token that will never expire. This is useful for server-to-server communication, but is also less secure than the JWT token.
* @param string $token
*/
$directus->token('ThIs_Is_ThE_tOkEn');
$items = $directus->items($collection)->get();
if($directus->isError($items)) {
// The request failed.
}
/**
* List the Items
* @param string $collection
*/
$items = $directus->items($collection)->get();
/**
* Fields
* Optional
* Control what fields are being returned in the object.
* @param string $fields
* _fields("fields1,field2,...")
*/
$items = $directus->items($collection)->_fields("field1,field2")->get();
/**
* Limit
* Optional
* A limit on the number of objects that are returned. Default is 200
* @param integer $limit
* _limit(10)
*/
$items = $directus->items($collection)->_limit(10)->get();
/**
* Offset
* Optional
* How many items to skip when fetching data. Default is 0.
* @param integer $offset
* _offset(10)
*/
$items = $directus->items($collection)->_offset(10)->get();
/**
* Sort
* Optional
* How to sort the returned items.
* @param string $sort
* _sort("-id,sort")
*/
$items = $directus->items($collection)->_sort("-id,sort")->get();
/**
* Single
* Optional
* Return the result as if it was a single item. Useful for selecting a single item based on filters and search queries. Will force limit to be 1.
* _single()
*/
$items = $directus->items($collection)->_single()->get();
/**
* Status
* Optional
* Filter items by the given status.
* @param string $status
* _status("published,under_review,draft")
*/
$items = $directus->items($collection)->_status("published,under_review,draft")->get();
/**
* Filter
* Optional
* Select items in collection by given conditions.
* @param array $filter
* _filter(["page_id" => $id])
*/
$items = $directus->items($collection)->_filter(["page_id" => $id])->get();
/**
* Retrieve an Item
* @param string $collection
* @param integer $id
*/
$item = $directus->item($collection, $id)->get();
/**
* Create an Item
* @param string $collection
* @param array $params
*/
$directus->items($collection)->create([
'title' => 'The Post Title',
'status' => 'Published'
]);
/**
* Update an Item
* @param string $collection
* @param integer $id
* @param array $params
*/
$directus->item($collection, $id)->update([
'title' => 'The New Post Title'
]);
/**
* Delete an Item
* @param string $collection
* @param integer $id
*/
$directus->item($collection, $id)->delete();
/**
* List Item Revisions
* @param string $collection
* @param integer $id
*/
$revisions = $directus->itemRevisions($collection, $id)->get();
/**
* Revert to a Given Revision
* @param string $collection
* @param integer $id
* @param integer $revision
*/
$directus->itemRevert($collection, $id, $revision)->update();
/**
* Get an asset
* @param string $private_hash
*/
$asset = $directus->asset($private_hash)->get();
/**
* Get an asset
* @param string $private_hash
* @param string $key - The key of the asset size configured in settings.
*
* @param integer $w
* Width of the file in pixels.
*
* @param integer $h
* Height of the file in pixels.
*
* @param string $f
* Fit. One of crop, contain.
*
* @param integer $q
* Quality of compression. Number between 1 and 100.
*/
$asset = $directus->asset($private_hash)->queries([
'key' => $key,
'w' => 100,
'h' => 100,
'f' => 'crop',
'q' => 80
])->get();
/**
* List the Revisions
*/
$revisions = $directus->revisions()->get();
/**
* Retrieve a Revision
* @param integer $id
*/
$revision = $directus->revision($id)->get();
/**
* List the Roles
*/
$roles = $directus->roles()->get();
/**
* Retrieve a Role
* @param integer $id
*/
$role = $directus->role($id)->get();
/**
* Create a Role
* @param array $data
*/
$directus->roles()->create([
'name' => 'Interns'
]);
/**
* Update a Role
* @param integer $id
* @param array $data
*/
$directus->role($id)->update([
'description' => 'Limited access only.'
]);
/**
* Delete a Role
* @param integer $id
*/
$directus->role($id)->delete();
/**
* Retrieve Server Info
* Perform a system status check and return the options.
* @param string $token
*/
$info = $directus->info($token)->get();
/**
* Ping the server
* Ping, pong. Ping.. pong. 🏓
*/
$pong = $directus->ping()->get();
/**
* List the Settings
* The Setting Object
*/
$settings = $directus->settings()->get();
/**
* Retrieve a Setting
* @param integer $id
*/
$setting = $directus->setting($id)->get();
/**
* Create a Setting
* @param array $data
*/
$directus->settings()->create([
'key' => 'my_custom_setting',
'value' => 12
]);
/**
* Update a Setting
* @param integer $id
*/
$directus->setting($id)->update([
'value' => 15
]);
/**
* Delete a Setting
* @param integer $id
*/
$directus->setting($id)->delete();
/**
* List the users
*/
$users = $directus->users()->get();
/**
* Retrieve a User
* @param integer $id
*/
$user = $directus->user($id)->get();
/**
* Retrieve the Current User
*/
$me = $directus->me()->get();
/**
* Create a User
* Create a new user.
* @param array $data
*/
$directus->users()->create([
'first_name' => 'John',
'last_name' => 'Smith',
'email' => '[email protected]',
'password' => 'VeryStrongPassword',
'role' => 1,
'status' => 'active'
]);
/**
* Update a User
* Update an existing user
* @param integer $id
* @param array $data
*/
$directus->user($id)->update([
'status' => 'suspended'
]);
/**
* Delete a User
* Delete an existing user
* @param integer $id
*/
$directus->user($id)->delete();
/**
* Invite a New User
* Invites one or more users to this project. It creates a user with an invited status, and then sends an email to the user with instructions on how to activate their account.
* @param integer $email
*/
$directus->invite('[email protected]')->create();
/**
* Accept User Invite
* Accepts and enables an invited user using a JWT invitation token.
* @param string $token
*/
$directus->acceptUser($token)->post();
/**
* Track the Last Used Page
* Updates the last used page field of the user. This is used internally to be able to open the Directus admin app from the last page you used.
* @param integer $id
* @param string $url
*/
$directus->trackingPage($id, '/thumper/settings/')->update();
/**
* List User Revisions
* List the revisions made to the given user.
* @param integer $id
*/
$revisions = $directus->userRevisions($id)->get();
/**
* Retrieve a User Revision
* Retrieve a single revision of the user by offset.
* @param integer $id
* @param integer $offset
*/
$revision = $directus->userRevision($id, $offset)->get();
/**
* Create a Hash
* Create a hash for a given string.
* @param string $StringToHash
*/
$hash = $directus->hash('StringToHash')->create();
/**
* Verify a Hashed String
* Check if a hash is valid for a given string.
* @param string $StringToHash
* @param string $hash
*/
$valid = $directus->hashMatch('StringToHash', $hash)->create();
/**
* Generate a Random String
* Returns a random string of given length.
* @param integer $length
*/
$string = $directus->randomString($length)->create();
/**
* Generate a 2FA Secret
* Returns a random string that can be used as a 2FA secret
*/
$secret = $directus->secret()->get();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.