Using Zoho Remote API in PHP to edit office documents on your LAMP server

Check out the new site at https://rkblog.dev.

Using Zoho Remote API with Python is quite similar to using it with PHP. In both languages we use the CURL library. In PHP the usage of curl is a bit different

Requirements

  • Zoho API key
  • PHP hosting with CURL support

Remote API for Zoho Writer

Here is an example script that will open local document in Zoho Editor:
<?php
$api = 'Your API KEY';

$fields = array();
$fields['content'] = "@/path/to/file.odt";
$fields['filename'] = 'file.odt';
$fields['id'] = '13';
$fields['format'] = 'odt';
$fields['saveurl'] = urlencode('http://localhost/foo.php');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://export.writer.zoho.com/remotedoc.im?apikey='.$api.'&output=editor');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_VERBOSE,  1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$page = curl_exec($ch);
curl_close($ch);
echo $page;
We use the CURL library to POST the file and all required data. content is the document file content (for Curl use the path to file with @ at start), filename which is the file name of the document, id - unique ID used to identify the document (on saves for example), format is the file format in which the document will be saved, saveurl is the full URL on which the saved document will be posted to (on our server, that will handle receiving the new version of the document, example is on zoho API wiki... of course it won't work for localhosts). The API will return a JavaScript code that will redirect to the editor.

Remote API for Sheet and Show

Remote API for Sheet and Show is bit simpler. In this case the API won't return JS code that opens the editor, but will send the URL to the editor in Location response header. So we have to get the header:
<?php
$api = 'Your API KEY';

$fields = array();
$fields['content'] = "@/path/to/b.ppt";
$fields['filename'] = 'b.ppt';
$fields['id'] = '13';
$fields['format'] = 'ppt';
$fields['saveurl'] = urlencode('http://localhost/foo.php');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://show.zoho.com/remotedoc.im?apikey='.$api.'&output=editor');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_VERBOSE,  1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);

$content = curl_exec ($ch);
curl_close ($ch);

$headers = explode("
", $content);
foreach($headers as $val)
{
if (stripos($val, 'Location: ') !== false)
	{
	$url = str_replace('Location: ', '', $val);
	echo '<iframe src="'.$url.'" style="width:100%;height:400px;"></iframe>';
	}
}
In this example we get the response headers, and not response body. We check headers for the "Location" header and extract the URL from it so we can for example open the editor in an iframe.
RkBlog

PHP Tutorials and Scripts, 9 October 2009


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact