Difference between revisions of "PHP client - OpenKM 5.1"

From OpenKM Documentation
Jump to: navigation, search
(List documents)
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{TOCright}} __TOC__
 +
 +
== Remote method info ==
 +
<source lang="php">
 +
<?php
 +
  // Register WSDL
 +
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/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>**** FUNTIONS ****<br>";
 +
  foreach ($OKMAuth->__getFunctions() as $function) {
 +
    echo $function."<br>";
 +
  }
 +
 +
  echo "<br>**** TYPES ****<br>";
 +
  foreach ($OKMAuth->__getTypes() as $types) {
 +
    echo $types."<br>";
 +
  }
 +
?>
 +
</source>
 +
 
== Authentication ==
 
== Authentication ==
  
Line 15: Line 40:
 
</source>
 
</source>
  
== List documents ==
+
== List folders and documents ==
  
 
<source lang="php">
 
<source lang="php">
Line 40: Line 65:
 
   // List folders
 
   // List folders
 
   $folderArrayResult = $OKMFolder->getChilds($token, $path);
 
   $folderArrayResult = $OKMFolder->getChilds($token, $path);
 +
 
 +
  // Starting with OpenKM 5.0 should be $folderArrayResult->item
 
   $folderArray = $folderArrayResult->value;
 
   $folderArray = $folderArrayResult->value;
  
Line 54: Line 81:
 
   // List documents
 
   // List documents
 
   $documentArrayResult = $OKMDocument->getChilds($token, $path);
 
   $documentArrayResult = $OKMDocument->getChilds($token, $path);
 +
 
 +
  // Starting with OpenKM 5.0 should be $documentArrayResult->item
 
   $documentArray = $documentArrayResult->value;
 
   $documentArray = $documentArrayResult->value;
  
Line 72: Line 101:
  
 
== Create document ==
 
== Create document ==
 +
=== Using default create ===
 +
<source lang="php">
 +
<?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, 'convertibleToDxf' => 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);
 +
?>
 +
</source>
 +
 +
=== Using simple create ===
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
  // Register WSDL
 
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
 
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
 +
  $OKMDocument = new SoapClient('http://localhost:8080/OpenKM/OKMDocument?wsdl');
 +
  $OKMProperty = new SoapClient('http://localhost:8080/OpenKM/OKMProperty?wsdl');
 +
  $file = '/etc/hosts';
 +
 +
  // Login
 
   $token = $OKMAuth->login('okmAdmin','admin');
 
   $token = $OKMAuth->login('okmAdmin','admin');
   echo "Token: ".$token;
+
   echo "Token: ".$token."<br>";
 +
 
 +
  // Create document
 +
  $newDoc = $OKMDocument->createSimple($token, '/okm:root/hosts.txt', file_get_contents($file));
 +
  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)
 +
  $OKMProperty->addCategory($token, $newDoc->path, 'fcfbc570-f7a3-43a8-9c98-4807abc8156b');
 +
 
 +
  // Logout
 
   $OKMAuth->logout($token);
 
   $OKMAuth->logout($token);
 
?>
 
?>
 
</source>
 
</source>
  
== List folders ==
+
== Perform search ==
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
  // Register WSDL
 
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?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');
 
   $token = $OKMAuth->login('okmAdmin','admin');
   echo "Token: ".$token;
+
   echo "Token: ".$token."<br>";
 +
 
 +
  $queryResultArrayResult = $OKMSearch->findByContent($token, '***');
 +
 
 +
  // Starting with OpenKM 5.0 should be $queryResultArrayResult->item
 +
  $queryResultArray = $queryResultArrayResult->value;
 +
 +
  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($token);
 
   $OKMAuth->logout($token);
 
?>
 
?>
 
</source>
 
</source>
  
== Create folder ==
+
And this is another sample of search, working for OpenKM 5.0 and better:
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
  // Register WSDL
 
   $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?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');
 
   $token = $OKMAuth->login('okmAdmin','admin');
   echo "Token: ".$token;
+
   echo "Token: ".$token."<br>\n";
 +
 
 +
  $qp = new QueryParams();
 +
  $qp->domain = 1; // DOCUMENT = 1; FOLDER = 2; MAIL = 4;
 +
  $qp->content = '***';
 +
  $queryResultArrayResult = $OKMSearch->find($token, $qp);
 +
  $queryResultArray = $queryResultArrayResult->item;
 +
 +
  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($token);
 +
 
 +
  class QueryParams {
 +
    var $content;
 +
    var $dashboard = false;
 +
    var $domain = 0;
 +
    var $id;
 +
    var $properties = array();
 +
  }
 +
 
 +
  class formElementComplexArray {
 +
    var $item;
 +
  }
 +
?>
 +
</source>
 +
 
 +
== Set property group value ==
 +
<source lang="php">
 +
<?php
 +
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/OKMAuth?wsdl');
 +
  $OKMPropertyGroup = new SoapClient('http://localhost:8080/OpenKM/OKMPropertyGroup?wsdl');
 +
 
 +
  $fec = new FormElementComplex();
 +
  $fec->objClass = "com.openkm.bean.form.Input";
 +
  $fec->name = "okp:technology.comment";
 +
  $fec->value = "Other comment from PHP";
 +
  $fecArray = new FormElementComplexArray();
 +
  $fecArray->item = $fec;
 +
 
 +
  $token = $OKMAuth->login('okmAdmin', 'admin');
 +
  $docPath = "/okm:root/salida.pdf";
 +
  $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>
 +
 +
== Exception handling ==
 +
<source lang="php">
 +
<?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);
 +
  }
 +
?>
 +
</source>
 +
 +
== Proxy configuration ==
 +
<source lang="php">
 +
$client = new SoapClient("some.wsdl", array('proxy_host' => "https://example.org", 'proxy_port' => 443);
 
</source>
 
</source>
  
 
[[Category: Webservices Guide]]
 
[[Category: Webservices Guide]]
[[Category:OKM Network]]
 

Latest revision as of 18:34, 1 December 2012

Remote method info

<?php
  // Register WSDL
  $OKMAuth = new SoapClient('http://localhost:8080/OpenKM/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>**** FUNTIONS ****<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/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);
  
  // Starting with OpenKM 5.0 should be $folderArrayResult->item
  $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);
  
  // Starting with OpenKM 5.0 should be $documentArrayResult->item
  $documentArray = $documentArrayResult->value;

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

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

Create document

Using default create

<?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, 'convertibleToDxf' => 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);
?>

Using simple create

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

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

  // Create document
  $newDoc = $OKMDocument->createSimple($token, '/okm:root/hosts.txt', file_get_contents($file));
  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)
  $OKMProperty->addCategory($token, $newDoc->path, 'fcfbc570-f7a3-43a8-9c98-4807abc8156b');

  // 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, '***');
  
  // Starting with OpenKM 5.0 should be $queryResultArrayResult->item
  $queryResultArray = $queryResultArrayResult->value;
 
  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($token);
?>

And this is another sample of search, working for OpenKM 5.0 and better:

<?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>\n";
  
  $qp = new QueryParams();
  $qp->domain = 1; // DOCUMENT = 1; FOLDER = 2; MAIL = 4;
  $qp->content = '***';
  $queryResultArrayResult = $OKMSearch->find($token, $qp);
  $queryResultArray = $queryResultArrayResult->item;
 
  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($token);
  
  class QueryParams {
    var $content;
    var $dashboard = false;
    var $domain = 0;
    var $id;
    var $properties = array();
  }
  
  class formElementComplexArray {
    var $item;
  }
?>

Set property group value

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

  $fec = new FormElementComplex();
  $fec->objClass = "com.openkm.bean.form.Input";
  $fec->name = "okp:technology.comment";
  $fec->value = "Other comment from PHP";
  $fecArray = new FormElementComplexArray();
  $fecArray->item = $fec;

  $token = $OKMAuth->login('okmAdmin', 'admin');
  $docPath = "/okm:root/salida.pdf";
  $OKMPropertyGroup->setProperties($token, $docPath, 'okg:technology', $fecArray);
  $OKMAuth->logout($token);

  class FormElementComplex {
    var $objClass;
    var $name;
    var $value;
    var $readonly;
  }

  class FormElementComplexArray {
    var $item;
  }
?>

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);
  }
?>

Proxy configuration

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