Playing around with Zoho Writer Remote API

I've been looking for a way to embed a quality document editor in an application I'm developing, and the pickings are slim. Google only has an API for Spreadsheets, and there are just about no options out there for people looking to host something yourself. ThinkFree Server is a fairly nice option, but you pay-per-user which is a hassle, and it requires Java for Firefox (I didn't look at IE) which is a bigger hassle. Enter Zoho.

I haven't used Zoho before today, but they have a nice API for interacting with their online storage, and provide an option to load and save remotely. Unfortunately they have no samples of the remote API available, and the Writer remote API is thin (at best).

Zoho expects a file to look like this:

  • content – Raw document content or blank for a new document
  • filename – the name Zoho will pass to the remote server when saving or use when downloading
  • id – unique id, used by the remote server
  • format – doc, html, pdf, sxw, odt, rtf, txt

If you're creating a new filename you will need to POST an empty string for content and an id and filename to use. To open a document you will need to provide the format of the file and the file's contents. I'm not sure what the maximum content size is. You also need to provide a URL (saveurl) for Zoho to use for saving the document, and a value for persistence, whose purpose isn't quite clear.

Since you need a public web server to save documents, you can't use this for an internal application out of the box. When you save the document Zoho POSTs to your saveurl with the filename, id, format and the file.

I threw something quickly together using PHP that does the following:

  • Provide a basic interface to create a new document or open an existing document
  • Load a document in the Zoho interface
  • Allow Zoho to remotely save a document

The database schema is very simple, and supports basic versioning.

CREATE TABLE `documents` (
  `content` longblob NOT NULL,
  `filename` varchar(255) NOT NULL,
  `format` char(4) NOT NULL,
  `id` varchar(100) NOT NULL,
  `revision` int(11) NOT NULL,
  `saveDate` timestamp NOT NULL default CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Usage

To create a new untitled and empty document only takes a few lines of PHP:

require_once('config.inc.php');
 
$writer = new ZohoRemoteWriter($GLOBALS['conf']);
$content = $writer->open_doc_in_zoho();
 
echo $content;

Opening an existing document takes one additional line of code:

require_once('config.inc.php');
 
$writer = new ZohoRemoteWriter($GLOBALS['conf']);
$writer->doc->load_by_id('doc_id_here')
$content = $writer->open_doc_in_zoho();
 
echo $content;

Limitations

  • The document format is hardcoded to Word (doc), but it is trivial to change this.
  • If your webserver is not publicly accessible or running on port 80 you will be unable to save any documents.
  • You will not be able to save files larger than the value of PHP's post_max_size and upload_max_filesize variables.
  • Files larger than MySQL's max_allowed_packet need to be chunked, there is some commented out (and untested) code in zoho-remote-writer.class.php for this. Also see the mysqli function send_long_data.
  • A new revision is added to the database each time the document is saved, this will use up a lot of disk space fairly soon.

Download

I wrote this on my development server running Apache 2, PHP 5.2 and MySQL 5.0 with the mysqli extension. It won't work with PHP <5 and MySQL <4.1.

The latest version (August 1, 2007, rev 925) is available for download here: zoho-remote-writer.zip

 

12 Responses to “Playing around with Zoho Writer Remote API”

  1. corey gilmore's blog Says:

    Playing around with Zoho Writer Remote API – Part 2…

    Following up on my post last night, I made a few changes to the example pages and added the ability to create a new Zoho Writer document by uploading (importing) a document (rtf, doc, odt, txt, html, sxw supported). The included PHP class now allows y…

  2. poorani Says:

    hi,

    How to use zoho-remote-writer.zip code to integrate zoho writer to my application

  3. vimala Says:

    Ya, the above code will be very much usefull for Remote API of ZOHO. But is there any code available in java for doing the same task. I don't know php. So i couldn't understand all those stuffs explained in the above program

  4. pat Says:

    Do you have an example page?

  5. Corey Says:

    Not a public one, I didn't feel like making the changes necessary to use it publicly (e.g. daily pruning of the database, limiting POST size). If you've got PHP and MySQL installed the sample code contains everything you need.

  6. pat Says:

    Sooo I havn't had time to use PHP 5.0+ yet (still on 4.0). To get it to work with my hosting company (1and1), I had to set all names to end w/ .php5 (i updated the references within the files as well). I'm getting an error now:

    Warning: mysqli::query() [function.mysqli-query]: Couldn't fetch mysqli in /homepages/15/d16/htdocs/Testing/TestZoho/zoho-remote-writer.class.php5 on line 255

    Fatal error: Call to a member function close() on a non-object in /homepages/15/d16/htdocs/Testing/TestZoho/zoho-remote-writer.class.php5 on line 261

    Any ideas?

  7. pat Says:

    Sorry to clutter up your message board… fixed the problem, something stupid on my end.

    To have this embedded in a website so that it still has my template around the editor- I'm thinking an iFrame? Any other suggestions?

  8. pat Says:

    You have a HUGE bug in your code. It centers around the versioning.

    zoho-remote-writer.class.php lines 235 -> 245.

    The line
    '$this->doc->load_by_id($document->id)' overwrites the 'new' text sent by zoho w/ the 'old' text from the database. The reason is, is that $this references the same object as $document.

    It's invoked by save.php line 9:
    $writer->receive_zoho_doc($writer->doc, isset($_FILES['content']['tmp_name']) ? $_FILES['content']['tmp_name'] : FALSE );

    (function defintion):
    function receive_zoho_doc($document, $file=FALSE)

    sooo it's calling itself w/ a parameter that is referencing itself.

    PPPPPLLLEEEAAASSSEEEEE correct me if i'm wrong or tell me if I'm right. I've spent sooooooo many hours trying to figure this out.

    ———

    To see the problem, just add this code below. It will shoot you an email w/ the content from before and after the statement (hint: it's at the bottom of the email)

    $Header = "From: " . 'someone@asdfasdfcom' . "\r\n" .
    "Reply-To:" . 'another_person@asdfasdf.com' . "\r\n" ;
    mail('youremail!!!!!', 'test subject – ZOHO1', 'one'. var_export(debug_backtrace(), true) ."\n".$document->content, $Header);

    }
    $document->revision = 999;
    // Increment the current revision by one.
    if( $this->doc->load_by_id($document->id) ) {

    $Header = "From: " . 'someone@asdf.com' . "\r\n" .
    "Reply-To:" . 'another_person@asdf.com' . "\r\n" ;
    mail('youremail!!!!!!!', 'test subject – ZOHO2', 'two'. var_export(debug_backtrace(), true) ."\n".$document->content, $Header);
    $document->revision = $this->doc->revision
    …..

  9. pat Says:

    working code:
    function receive_zoho_doc($document, $file)
    {
    if( $file )
    {
    $document->content = file_get_contents($file);
    }
    $dummy = new ZohoRemoteWriter($GLOBALS['conf']);

    // Increment the current revision by one.
    if( $dummy->doc->load_by_id($document->id) )
    {
    $document->revision = $dummy->doc->revision + 1;
    }

    $document->save();
    }

  10. Richard G Says:

    Hi, I am new to PHP and attempting to get this example to work.

    I am currently getting the following error "WARNING=File Extension not supported RESULT=FALSE ERROR_CODE=1842"

    Any help towards fixing this error would be much appreciated.

    Thank you.

    Rich.

  11. nt Says:

    I am also getting the "WARNING=File Extension not supported RESULT=FALSE ERROR_CODE=1842" error. Any advice?

    Thank you!

  12. Samrat Banerjee Says:

    the above code will be very much useful for Remote API of ZOHO. But is there any code available in ASP.NET for doing the same task.

Leave a Reply


© 2007-2012, Corey Gilmore | Posts RSS Feed | Comments RSS Feed | Contact

 

The views expressed on these pages are mine alone and not those of any past or present employer. All information presented on this site was obtained lawfully and not through disclosure under the terms of an NDA.