I've got some documents in OKM that I want to make available on a custom web page. I want to use the getContent method to allow a user to save the document locallaly to their computer. I am able to list documents in a folder via PHP, but I can't figure out how to write the byte array returned by getContent to disk. Has anyone done this before that can point me in the right direction? Can I do this with PHP, or do I need to write the file with a client side language, like Javascript? Thanks.
Here's my code:
Code: Select all
<?php
// Register WSDL
$OKMAuth = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMAuth?wsdl');
$OKMDocument = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMDocument?wsdl');
$OKMFolder = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMFolder?wsdl');
$path = '/okm:root/Customer Docs/olsonea@gmail.com';
// Login
$loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
$token = $loginResp->return;
echo "Token: ".$token."<br>";
//open document test
$docPath = '/okm:root/Customer Docs/foo/Invoice-0000034.pdf';
echo "Document: ".$docPath."<br>";
//get document content
$getDocumentResp = $OKMDocument->getContent(array('token' => $token, 'docPath' => $docPath, checkout => 0));
echo "Binary Length: ".count($getDocumentResp)."<br>";
echo "Binary Data:<br>".$getDocumentResp->return."<br><br>";
//convert output from byte[] to string
//$bytes = $getDocumentResp->return;
$docstring = call_user_func_array("pack", array_merge(array("C*"), $getDocumentResp->return));
echo "String Data:<br>".$docstring."<br>";
$strlen = strlen($docstring);
echo "String Length: ".$strlen."<br>";
//write output to disk
$fp = fopen('test.pdf','w');
$fwrite = fwrite($fp,$docstring,$strlen);
$fclose($fp);
//access the file and echo back the data
$data = file_get_contents('test.pdf');
echo "Written Data:<br>".$data."<br>";
// Logout
$OKMAuth->logout(array('token' => $token));
echo "Logged out.";
?>