Difference between revisions of "PHP client - OpenKM 6.2"

From OpenKM Documentation
Jump to: navigation, search
(Using default create)
(List folders and documents)
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{TOCright}} __TOC__
 
{{TOCright}} __TOC__
 +
 +
{{Note|The is available a PHP class which ease the use of OpenKM API at http://code.google.com/p/openkm-php-class/.}}
  
 
== Remote method info ==
 
== Remote method info ==
Line 12: Line 14:
 
   ini_set('soap.wsdl_cache', 0);
 
   ini_set('soap.wsdl_cache', 0);
  
   echo "<br>**** FUNTIONS ****<br>";
+
   echo "<br>**** FUNCTIONS ****<br>";
 
   foreach ($OKMAuth->__getFunctions() as $function) {
 
   foreach ($OKMAuth->__getFunctions() as $function) {
 
     echo $function."<br>";
 
     echo $function."<br>";
Line 42: Line 44:
  
 
== List folders and documents ==
 
== List folders and documents ==
 +
As noted in the bug report http://bugs.php.net/bug.php?id=36226, it is considered a feature that sequences with a single element do not come out as arrays. To override this "feature" you can do the following:
 +
 +
<source lang="php">
 +
$OKMFolder = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));
 +
</source>
  
 
<source lang="php">
 
<source lang="php">
Line 130: Line 137:
 
</source>
 
</source>
  
=== Using simple create ===
+
=== Using create simple ===
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 
   // Register WSDL
 
   // Register WSDL
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
+
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
   $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/OKMDocument?wsdl');
+
   $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/services/OKMDocument?wsdl');
   $OKMProperty = new SoapClient('http://localhost:8080/OpenKM/OKMProperty?wsdl');
+
   $OKMProperty = new SoapClient('http://localhost:8080/OpenKM/services/OKMProperty?wsdl');
 +
  $OKMRepository = new SoapClient('http://localhost:8080/OpenKM/services/OKMRepository?wsdl');
 
   $file = '/etc/hosts';
 
   $file = '/etc/hosts';
  
 
   // Login
 
   // Login
   $token = $OKMAuth->login('okmAdmin','admin');
+
   $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
 +
  $token = $loginResp->return;
 
   echo "Token: ".$token."<br>";
 
   echo "Token: ".$token."<br>";
  
 
   // Create document
 
   // Create document
   $newDoc = $OKMDocument->createSimple($token, '/okm:root/hosts.txt', file_get_contents($file));
+
   $createResp = $OKMDocument->createSimple(array('token' => $token, 'docPath' => '/okm:root/hosts.txt', 'content' => file_get_contents($file)));
 +
  $newDoc = $createResp->return;
 
   echo "[DOCUMENT] Path: ".$newDoc->path.", Author: ".$newDoc->author.", Size: ".$newDoc->actualVersion->size."<br>";
 
   echo "[DOCUMENT] Path: ".$newDoc->path.", Author: ".$newDoc->author.", Size: ".$newDoc->actualVersion->size."<br>";
  
   // Category assign (change UUID to category UUID found in the category properties tab)
+
   // Category assign
   $OKMProperty->addCategory($token, $newDoc->path, 'fcfbc570-f7a3-43a8-9c98-4807abc8156b');
+
  $getNodeUuid = $OKMRepository->getNodeUuid(array('token' => $token, 'path' => '/okm:categories/alfa'));
 +
  $catId = $getNodeUuid->return;
 +
   $OKMProperty->addCategory(array('token' => $token, 'nodePath' => $newDoc->path, 'catId' => $catId));
  
 
   // Logout
 
   // Logout
Line 156: Line 168:
  
 
== Perform search ==
 
== Perform search ==
 +
=== Search by content ===
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 
   // Register WSDL
 
   // Register WSDL
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
+
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
   $OKMSearch = new SoapClient('http://localhost:8080/OpenKM/OKMSearch?wsdl');
+
   $OKMSearch = new SoapClient('http://localhost:8080/OpenKM/services/OKMSearch?wsdl');
 
   
 
   
 
   // Login
 
   // Login
   $token = $OKMAuth->login('okmAdmin','admin');
+
   $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
 +
  $token = $loginResp->return;
 
   echo "Token: ".$token."<br>";
 
   echo "Token: ".$token."<br>";
 
+
   $queryResultArrayResult = $OKMSearch->findByContent($token, '***');
+
   $findByContentResp = $OKMSearch->findByContent(array('token' => $token, 'content' => 'grial'));
 
+
   $queryResultArray = $findByContentResp->return;
  // Starting with OpenKM 5.0 should be $queryResultArrayResult->item
 
   $queryResultArray = $queryResultArrayResult->value;
 
 
   
 
   
 
   if ($queryResultArray) {
 
   if ($queryResultArray) {
Line 182: Line 194:
 
   
 
   
 
   // Logout
 
   // Logout
   $OKMAuth->logout($token);
+
   $OKMAuth->logout(array('token' => $token));
 
?>
 
?>
 
</source>
 
</source>
  
And this is another sample of search, working for OpenKM 5.0 and better:
+
=== Complex search ===
 +
This is another sample of complex search:
 +
 
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 
   // Register WSDL
 
   // Register WSDL
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
+
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
   $OKMSearch = new SoapClient('http://localhost:8080/OpenKM/OKMSearch?wsdl');
+
   $OKMSearch = new SoapClient('http://localhost:8080/OpenKM/services/OKMSearch?wsdl');
 
   
 
   
 
   // Login
 
   // Login
   $token = $OKMAuth->login('okmAdmin','admin');
+
   $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
 +
  $token = $loginResp->return;
 
   echo "Token: ".$token."<br>\n";
 
   echo "Token: ".$token."<br>\n";
 
+
 
   $qp = new QueryParams();
 
   $qp = new QueryParams();
 
   $qp->domain = 1; // DOCUMENT = 1; FOLDER = 2; MAIL = 4;
 
   $qp->domain = 1; // DOCUMENT = 1; FOLDER = 2; MAIL = 4;
   $qp->content = '***';
+
   $qp->content = 'grial';
   $queryResultArrayResult = $OKMSearch->find($token, $qp);
+
   $findResp = $OKMSearch->find(array('token' => $token, 'params' => $qp));
   $queryResultArray = $queryResultArrayResult->item;
+
   $queryResultArray = $findResp->return;
+
 
 
   if ($queryResultArray) {
 
   if ($queryResultArray) {
 
     if (is_array($queryResultArray)) {
 
     if (is_array($queryResultArray)) {
Line 214: Line 229:
 
   
 
   
 
   // Logout
 
   // Logout
   $OKMAuth->logout($token);
+
   $OKMAuth->logout(array('token' => $token));
 
+
 
   class QueryParams {
 
   class QueryParams {
     var $content;
+
     var $content = '';
 
     var $dashboard = false;
 
     var $dashboard = false;
 
     var $domain = 0;
 
     var $domain = 0;
     var $id;
+
     var $id = 0;
 
     var $properties = array();
 
     var $properties = array();
  }
 
 
 
  class formElementComplexArray {
 
    var $item;
 
 
   }
 
   }
 
?>
 
?>
Line 233: Line 244:
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
+
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
   $OKMPropertyGroup = new SoapClient('http://localhost:8080/OpenKM/OKMPropertyGroup?wsdl');
+
   $OKMPropertyGroup = new SoapClient('http://localhost:8080/OpenKM/services/OKMPropertyGroup?wsdl');
 
+
 
   $fec = new FormElementComplex();
+
   $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
   $fec->objClass = "com.openkm.bean.form.Input";
+
   $token = $loginResp->return;
   $fec->name = "okp:technology.comment";
+
  echo "Token: ".$token;
   $fec->value = "Other comment from PHP";
+
 
   $fecArray = new FormElementComplexArray();
+
  $docPath = "/okm:root/hosts.txt";
   $fecArray->item = $fec;
+
   $entry['key'] = 'okp:technology.comment';
 
+
   $entry['value'] = 'Other comment from PHP 3';
  $token = $OKMAuth->login('okmAdmin', 'admin');
+
   $properties = array($entry);
  $docPath = "/okm:root/salida.pdf";
+
   $OKMPropertyGroup->setPropertiesSimple(array('token' => $token, 'nodePath' => $docPath, 'grpName' => 'okg:technology', 'properties' => $properties));
  $OKMPropertyGroup->setProperties($token, $docPath, 'okg:technology', $fecArray);
 
 
   $OKMAuth->logout($token);
 
   $OKMAuth->logout($token);
 
  class FormElementComplex {
 
    var $objClass;
 
    var $name;
 
    var $value;
 
    var $readonly;
 
  }
 
 
  class FormElementComplexArray {
 
    var $item;
 
  }
 
 
?>
 
?>
 
</source>
 
</source>
Line 264: Line 263:
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
  // Register WSDL
 +
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
 +
  $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/services/OKMDocument?wsdl');
 +
 
 
   function format_exception($e) {
 
   function format_exception($e) {
 
     if (isset($e->detail)) {
 
     if (isset($e->detail)) {
Line 274: Line 277:
 
     return $exceptionName.": ".$e->faultstring;
 
     return $exceptionName.": ".$e->faultstring;
 
   }
 
   }
 
+
 
 
   try {
 
   try {
     $token = $OKMAuth->login('okmAdmin', 'admin');
+
     $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
     $document = $OKMDocument->getProperties($token, '/okm:root/hosts.txt');
+
    $token = $loginResp->return;
     print_r($document);
+
     $getPropertiesResp = $OKMDocument->getProperties(array('token' => $token, 'docPath' => '/okm:root/nofile.txt'));
 +
    $docProps = $getPropertiesResp->return;
 +
     print_r($docProps);
 
   } catch (Exception $e) {
 
   } catch (Exception $e) {
 
     echo format_exception($e);
 
     echo format_exception($e);
  } finally {
 
    $OKMAuth->logout($token);
 
 
   }
 
   }
 +
 
 +
  $OKMAuth->logout(array('token' => $token));
 
?>
 
?>
 
</source>
 
</source>
 +
 +
Note that '''finally''' will be included in PHP 5.5.
  
 
== Proxy configuration ==
 
== Proxy configuration ==

Latest revision as of 14:08, 26 March 2013


Nota clasica.png The is available a PHP class which ease the use of OpenKM API at http://code.google.com/p/openkm-php-class/.

Remote method info

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');

  // Disable WSDL cache
  ini_set('soap.wsdl_cache_enabled', 0);
  ini_set('soap.wsdl_cache_ttl', 0); 
  ini_set('soap.wsdl_cache', 0);

  echo "<br>**** FUNCTIONS ****<br>";
  foreach ($OKMAuth->__getFunctions() as $function) {
    echo $function."<br>";
  }

  echo "<br>**** TYPES ****<br>";
  foreach ($OKMAuth->__getTypes() as $types) {
    echo $types."<br>";
  }
?>

Authentication

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
 
  // Login
  $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
  $token = $loginResp->return;
  echo "Token: ".$token;
 
  // Logout
  $OKMAuth->logout(array('token' => $token));
?>

List folders and documents

As noted in the bug report http://bugs.php.net/bug.php?id=36226, it is considered a feature that sequences with a single element do not come out as arrays. To override this "feature" you can do the following:

$OKMFolder = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));
<?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/services/OKMAuth?wsdl');
  $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/services/OKMDocument?wsdl');
  $OKMFolder = new SoapClient('http://localhost:8080/OpenKM/services/OKMFolder?wsdl');
  $path = '/okm:root';

  // Login
  $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
  $token = $loginResp->return;
  echo "Token: ".$token."<br>";
  echo "Path: ".$path."<br>";

  // List folders
  $getChildrenResp = $OKMFolder->getChildren(array('token' => $token, 'fldPath' => $path));
  $folderArray = $getChildrenResp->return;

  if ($folderArray) {
    if (is_array($folderArray)) {
      foreach ($folderArray as $folder) {
        printFolder($folder);
      }
    } else {
      printFolder($folderArray);
    }
  }

  // List documents
  $getChildrenResp = $OKMDocument->getChildren(array('token' => $token, 'fldPath' => $path));
  $documentArray = $getChildrenResp->return;

  if ($documentArray) {
    if (is_array($documentArray)) {
      foreach ($documentArray as $document) {
        printDocument($document);
      }
    } else {
      printDocument($documentArray);
    }
  }

  // Logout
  $OKMAuth->logout(array('token' => $token));
?>

Create document

Using default create

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
  $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/services/OKMDocument?wsdl');
  $file = '/etc/hosts';

  // Login
  $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
  $token = $loginResp->return;
  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, 'convertibleToDxf' => false);

  $createResp = $OKMDocument->create(array('token' => $token, 'doc' => $doc, 'content' => file_get_contents($file)));
  $newDoc = $createResp->return;
  echo "[DOCUMENT] Path: ".$newDoc->path.", Author: ".$newDoc->author.", Size: ".$newDoc->actualVersion->size."<br>";

  // Logout
  $OKMAuth->logout(array('token' => $token));
?>

Using create simple

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
  $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/services/OKMDocument?wsdl');
  $OKMProperty = new SoapClient('http://localhost:8080/OpenKM/services/OKMProperty?wsdl');
  $OKMRepository = new SoapClient('http://localhost:8080/OpenKM/services/OKMRepository?wsdl');
  $file = '/etc/hosts';

  // Login
  $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
  $token = $loginResp->return;
  echo "Token: ".$token."<br>";

  // Create document
  $createResp = $OKMDocument->createSimple(array('token' => $token, 'docPath' => '/okm:root/hosts.txt', 'content' => file_get_contents($file)));
  $newDoc = $createResp->return;
  echo "[DOCUMENT] Path: ".$newDoc->path.", Author: ".$newDoc->author.", Size: ".$newDoc->actualVersion->size."<br>";

  // Category assign
  $getNodeUuid = $OKMRepository->getNodeUuid(array('token' => $token, 'path' => '/okm:categories/alfa'));
  $catId = $getNodeUuid->return;
  $OKMProperty->addCategory(array('token' => $token, 'nodePath' => $newDoc->path, 'catId' => $catId));

  // Logout
  $OKMAuth->logout($token);
?>

Perform search

Search by content

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
  $OKMSearch = new SoapClient('http://localhost:8080/OpenKM/services/OKMSearch?wsdl');
 
  // Login
  $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
  $token = $loginResp->return;
  echo "Token: ".$token."<br>";
 
  $findByContentResp = $OKMSearch->findByContent(array('token' => $token, 'content' => 'grial'));
  $queryResultArray = $findByContentResp->return;
 
  if ($queryResultArray) {
    if (is_array($queryResultArray)) {
      foreach ($queryResultArray as $queryResult) {
        echo "-> ".$queryResult->document->path." (".$queryResult->score.")<br>";
      }
    } else {
      echo "-> ".$queryResultArray->document->path." (".$queryResultArray->score.")<br>";
    }
  }
 
  // Logout
  $OKMAuth->logout(array('token' => $token));
?>

Complex search

This is another sample of complex search:

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
  $OKMSearch = new SoapClient('http://localhost:8080/OpenKM/services/OKMSearch?wsdl');
 
  // Login
  $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
  $token = $loginResp->return;
  echo "Token: ".$token."<br>\n";
 
  $qp = new QueryParams();
  $qp->domain = 1; // DOCUMENT = 1; FOLDER = 2; MAIL = 4;
  $qp->content = 'grial';
  $findResp = $OKMSearch->find(array('token' => $token, 'params' => $qp));
  $queryResultArray = $findResp->return;
  
  if ($queryResultArray) {
    if (is_array($queryResultArray)) {
      foreach ($queryResultArray as $queryResult) {
        echo "-> ".$queryResult->document->path." (".$queryResult->score.")<br>\n";
      }
    } else {
      echo "-> ".$queryResultArray->document->path." (".$queryResultArray->score.")<br>";
    }
  }
 
  // Logout
  $OKMAuth->logout(array('token' => $token));
 
  class QueryParams {
    var $content = '';
    var $dashboard = false;
    var $domain = 0;
    var $id = 0;
    var $properties = array();
  }
?>

Set property group value

<?php
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
  $OKMPropertyGroup = new SoapClient('http://localhost:8080/OpenKM/services/OKMPropertyGroup?wsdl');
  
  $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
  $token = $loginResp->return;
  echo "Token: ".$token;
  
  $docPath = "/okm:root/hosts.txt";
  $entry['key'] = 'okp:technology.comment';
  $entry['value'] = 'Other comment from PHP 3';
  $properties = array($entry);
  $OKMPropertyGroup->setPropertiesSimple(array('token' => $token, 'nodePath' => $docPath, 'grpName' => 'okg:technology', 'properties' => $properties));
  $OKMAuth->logout($token);
?>

Exception handling

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/services/OKMAuth?wsdl');
  $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/services/OKMDocument?wsdl');
  
  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 {
    $loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
    $token = $loginResp->return;
    $getPropertiesResp = $OKMDocument->getProperties(array('token' => $token, 'docPath' => '/okm:root/nofile.txt'));
    $docProps = $getPropertiesResp->return;
    print_r($docProps);
  } catch (Exception $e) {
    echo format_exception($e);
  }
  
  $OKMAuth->logout(array('token' => $token));
?>

Note that finally will be included in PHP 5.5.

Proxy configuration

$client = new SoapClient("some.wsdl", array('proxy_host' => "https://example.org", 'proxy_port' => 443);