Search code examples
phpsag

How to GET an attachment from a Apache CouchDB document with Sag?


I have a document with an image attachment myimg.jpg which I would like to GET using Sag.

In my browser I am able to retrieve this image if I visit this url: http://localhost:5984/mydb/thedocid/myimg.jpg.

Using sag I am able to retrieve documents, but unable to retrieve attachments. I have tried to retrieve the image like so:

$img = $sag->get('thedocid/myimg.jpg')->body;

Instead of retrieving the image PHP seems to become unresponsive. I also thought disabling JSON decode might solve it, but it still causes PHP to become unresponsive.

$sag->decode(false);
$img = $sag->get('thedocid/myimg.jpg');

What am I doing wrong? How does one properly retrieve an attachment using Sag?

EDIT: After quite some time the attachment has been retrieved. Why is it so slow? The attachment is merely 4kb.


Solution

  • I still do not know why my initial code was so unresponsive/extremely slow, but thanks to Dave's comment I got an alternative way to retrieve the document with the attachment:

    $doc = $sag->get('thedocid/?attachments=true')->body;
    $img = base64_decode($doc->_attachments->{'myimg.jpg'}->data);