A php automation client is made available on github (https://github.com/nuxeo/nuxeo-automation-php-client). You can use it and ask for commit rights on the project if you want to improve it or fix a bug.
The project contains the library (https://github.com/nuxeo/nuxeo-automation-php-client/tree/master/NuxeoAutomationClient) and some sample use cases here (https://github.com/nuxeo/nuxeo-automation-php-client/tree/master/tests).
Queries / Chains
In this examples we are using the php automation client to demonstrate how to invoke remote operations.
The following example is executing a simple query against a remote server: SELECT * FROM Document. The server will return a JSON document listing all selected documents.
| Code Block |
|---|
$client = new PhpAutomationClient('http://localhost:8080/nuxeo/site/automation');
$session = $client->getSession('Administrator','Administrator');
$answer = $session->newRequest("Document.Query")->set('params', 'query', "SELECT * FROM Document" )->sendRequest();
$documentsArray = $answer->getDocumentList();
$value = sizeof($documentsArray);
echo '<table>';
echo '<tr><TH>uid</TH><TH>Path</TH>
<TH>Type</TH><TH>State</TH><TH>Title</TH><TH>Download as PDF</TH>';
for ($test = 0; $test < $value; $test ++){
echo '<tr>';
echo '<td> ' . current($documentsArray)->getUid() . '</td>';
echo '<td> ' . current($documentsArray)->getPath() . '</td>';
echo '<td> ' . current($documentsArray)->getType() . '</td>';
echo '<td> ' . current($documentsArray)->getState() . '</td>';
echo '<td> ' . current($documentsArray)->getTitle() . '</td>';
echo '<td><form id="test" action="../tests/B5bis.php" method="post" >';
echo '<input type="hidden" name="data" value="'.
current($documentsArray)->getPath(). '"/>';
echo '<input type="submit" value="download"/>';
echo '</form></td></tr>';
next($documentsArray);
}
echo '</table>';
|
The class phpAutomationClient allows you to open a session with the getSession (return a session instance). Then, from the session, you can create a new request by using the same named function. The set function is used to configure your automation request, giving the chain or operation to call as well as the loading params, context, and input parts. At last, you send the request with the sendRequest function.
You can see here how to use the getters in order to retrieve information from the Document object, built from the request answer.
Using blobs
Attach Blob
In order to attach a blob, we have to send a Multipart Request to Nuxeo. The first part of the request will contain the body of the request (params, context ...) and the second part will contain the blob (as an input).
| Code Block |
|---|
$client = new PhpAutomationClient('http://localhost:8080/nuxeo/site/automation');
$session = $client->getSession('Administrator','Administrator');
$answer = $session->newRequest("Blob.Attach")->set('params', 'document', $path)
->loadBlob($blob, $blobtype)
->sendRequest();
|
That will attach the blob to an existing file. In order to send a blob, you use the loadBlob() function. If a blob is loaded using this function, the sendRequet() function will automatically create a multipart request. If you load many blobs without noticing a precise localization in params, the blobs will be send as an attachment of the file (not as a content).
Get a blob
in order to get a blog, you have to read the content of the 'tempfile' after using the appropriate headers.
| Code Block |
|---|
$client = new PhpAutomationClient('http://localhost:8080/nuxeo/site/automation');
$session = $client->GetSession('Administrator','Administrator');
$answer = $session->NewRequest("Blob.Get")->Set('input', 'doc: ' . $path)->SendRequest();
if (!isset($answer) OR $answer == false)
echo '$answer is not set';
else{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename.'.pdf');
readfile('tempstream');
}
|
This will download the blob placed in file:content of the Nuxeo file designed by $path.