PHP code example of nuxeo / nuxeo-php-client

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

    

nuxeo / nuxeo-php-client example snippets


$url = 'http://localhost:8080/nuxeo';

use Nuxeo\Client\NuxeoClient;

$client = new NuxeoClient($url, 'Administrator', 'Administrator');

// To define global schemas, global enrichers and global headers in general
$client = $client->schemas("dublincore", "common")
  ->enrichers('document', ['acls'])

// For defining all schemas
$client = $client->schemas("*");

// For changing authentication method

use Nuxeo\Client\Auth\PortalSSOAuthentication;
use Nuxeo\Client\Auth\TokenAuthentication;

// PortalSSOAuthentication with nuxeo-platform-login-portal-sso
$client = $client->setAuthenticationMethod(new PortalSSOAuthentication($secret, $username));

// TokenAuthentication
$client = $client->setAuthenticationMethod(new TokenAuthentication($token));

// OAuth2Authentication
// The PHP client doesn't implement OAuth2 authorization flow as 
// it depends completely on the architecture choices of your app.
// To help understanding and implement, please find a sample SF4 app under intergation/oauth2.
// Once the authorization flow is ready and you have an access token,
// you can use the OAuth2Authentication in the PHP client:
$client = $client->setAuthenticationMethod(new OAuth2Authentication($accessToken));

// Fetch the root document
$result = $client->automation('Repository.GetDocument')->param("value", "/")->execute();
// Type auto-detected and cast as Nuxeo\Client\Objects\Document

// Execute query
$operation = $client->automation('Repository.Query')->param('query', 'SELECT * FROM Document');
$result = $operation->execute();
// Type auto-detected and cast as Nuxeo\Client\Objects\Documents

use Nuxeo\Client\Objects\Blob\Blob;
use Nuxeo\Client\Objects\Blob\Blobs;

// To upload|download blob(s)

$fileBlob = Blob::fromFile('/local/file.txt', 'text/plain');
$blob = $client->automation('Blob.AttachOnDocument')->param('document', '/folder/file')->input($fileBlob)->execute(Blob::class);

$inputBlobs = new Blobs();
$inputBlobs->add('/local/file1.txt', 'text/plain');
$inputBlobs->add('/local/file2.txt', 'text/plain');
$blobs = $client->automation('Blob.AttachOnDocument')->param('xpath', 'files:files')->param('document', '/folder/file')->input($inputBlobs)->execute(Blobs::class);

$resultBlob = $client->automation('Document.GetBlob')->input('folder/file')->execute(Blob::class);

use Nuxeo\Client\Objects\Document;

class MyBusinessClass extends Nuxeo\Client\Objects\Document {
      ...
}

// Unserialize document in a custom class
$operation = $client->automation('Document.Fetch')->param('value', '0fa9d2a0-e69f-452d-87ff-0c5bd3b30d7d');
$result = $operation->execute(MyBusinessClass::class);

use Nuxeo\Client\Objects\Document;
use Nuxeo\Client\Objects\Operation\DocRef;

// Enforce type of a property
$doc = $client->automation('Document.Fetch')->param('value', '0fa9d2a0-e69f-452d-87ff-0c5bd3b30d7d')->execute(Document::class);
$property = $doc->getProperty('custom:related', DocRef::class);

// Fetch the root document
$document = $client->repository()->fetchDocumentRoot();

// Fetch document by path
$document = $client->repository()->fetchDocumentByPath('/folders_2');

// Create a document
$document = Objects\Document::create()
  ->setProperty('dc:title', 'Some title');

// Update a document
$repository = $client->repository(); 
$document = $repository->fetchDocumentByPath('/note_0');
document->setPropertyValue("dc:title", "note updated");
$repository->updateDocumentByPath('/note_0', $document);

// Delete a document
$client->repository()->deleteDocumentByPath('/note_2');

// Get current user used to connect to Nuxeo Server
/** @var \Nuxeo\Client\Objects\User\User $user */
$user = $client->userManager()->fetchCurrentUser();

// Create User
$userManager->createUser((new User())
      ->setUsername('my_login')
      ->setCompany('Nuxeo')
      ->setEmail('[email protected]')
      ->setFirstName('Thomas A.')
      ->setLastName('Anderson')
      ->setPassword('passw0rd'));

//Update user
$userManager->updateUser($user);

//Attach user to group
$userManager->attachGroupToUser('username', 'group_name');
$userManager->attachUserToGroup('group_name', 'username');

// Fetch current user workflow tasks
/** @var \Nuxeo\Client\Objects\Workflow\Tasks $tasks */
$tasks = $client->workflows()->fetchTasks();

  "  "nuxeo/nuxeo-php-client": "~2.0"
  }