Difference between revisions of "PHP client - OpenKM 5.1"
From OpenKM Documentation
(→Perform search) |
(→Create document) |
||
Line 84: | Line 84: | ||
$token = $OKMAuth->login('okmAdmin','admin'); | $token = $OKMAuth->login('okmAdmin','admin'); | ||
echo "Token: ".$token."<br>"; | echo "Token: ".$token."<br>"; | ||
− | |||
// Create document | // Create document |
Revision as of 18:30, 9 March 2010
Authentication
<?php
// Register WSDL
$OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
// Login
$token = $OKMAuth->login('okmAdmin','admin');
echo "Token: ".$token;
// Logout
$OKMAuth->logout($token);
?>
List folders and documents
<?php
function printFolder($folder) {
echo "[FOLDER] Path: ".$folder->path.", Author: ".$folder->author."<br>";
}
function printDocument($document) {
echo "[DOCUMENT] Path: ".$document->path.", Author: ".$document->author.", Size: ".$document->actualVersion->size."<br>";
}
// Register WSDL
$OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
$OKMDocument = new SoapClient('http://localhost:8080/OpenKM/OKMDocument?wsdl');
$OKMFolder = new SoapClient('http://localhost:8080/OpenKM/OKMFolder?wsdl');
$path = '/okm:root';
// Login
$token = $OKMAuth->login('okmAdmin','admin');
echo "Token: ".$token."<br>";
echo "Path: ".$path."<br>";
// List folders
$folderArrayResult = $OKMFolder->getChilds($token, $path);
$folderArray = $folderArrayResult->value;
if ($folderArray) {
if (is_array($folderArray)) {
foreach ($folderArray as $folder) {
printFolder($folder);
}
} else {
printFolder($folderArray);
}
}
// List documents
$documentArrayResult = $OKMDocument->getChilds($token, $path);
$documentArray = $documentArrayResult->value;
if ($documentArray) {
if (is_array($documentArray)) {
foreach ($documentArray as $document) {
printDocument($document);
}
} else {
printDocument($documentArray);
}
}
// Logout
$OKMAuth->logout($token);
?>
Create document
<?php
// Register WSDL
$OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
$OKMDocument = new SoapClient('http://localhost:8080/OpenKM/OKMDocument?wsdl');
$file = '/etc/hosts';
// Login
$token = $OKMAuth->login('okmAdmin','admin');
echo "Token: ".$token."<br>";
// Create document
$doc = array('path' => '/okm:root/hosts.txt', 'mimeType' => null,
'actualVersion' => null, 'author' => null, 'checkedOut' => false,
'created' => null, 'keywords' => 'nada', 'language' => null,
'lastModified' => null, 'lockInfo' => null, 'locked' => false,
'permissions' => 0, 'size' => 0, 'subscribed' => false, 'uuid' => null,
'convertibleToPdf' => false, 'convertibleToSwf' => false,
'compactable' => false, 'training' => false);
$newDoc = $OKMDocument->create($token, $doc, file_get_contents($file));
echo "[DOCUMENT] Path: ".$newDoc->path.", Author: ".$newDoc->author.", Size: ".$newDoc->actualVersion->size."<br>";
// Logout
$OKMAuth->logout($token);
?>
Perform search
<?php
// Register WSDL
$OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
$OKMSearch = new SoapClient('http://localhost:8080/OpenKM/OKMSearch?wsdl');
// Login
$token = $OKMAuth->login('okmAdmin','admin');
echo "Token: ".$token."<br>";
$queryResultArrayResult = $OKMSearch->findByContent($token, 'grial');
$queryResultArray = $queryResultArrayResult->value;
if ($queryResultArray) {
foreach ($queryResultArray as $queryResult) {
echo "-> ".$queryResult->document->path."(".$queryResult->score.")<br>";
}
}
// Logout
$OKMAuth->logout($token);
?>
Exception handling
<?php
function format_exception($e) {
if (isset($e->detail)) {
$reflectionObject = new ReflectionObject($e->detail);
$properties = $reflectionObject->getProperties();
$exceptionName = $properties[0]->name;
} else {
$exceptionName = "Exception";
}
return $exceptionName.": ".$e->faultstring;
}
try {
$token = $OKMAuth->login('okmAdmin', 'admin');
$document = $OKMDocument->getProperties($token, '/okm:root/hosts.txt');
print_r($document);
} catch (Exception $e) {
echo format_exception($e);
} finally {
$OKMAuth->logout($token);
}
?>