Page 1 of 1

Getting document preview using the API

Posted: Wed Sep 21, 2022 6:45 am
by crafter
I am trying to get a preview/thumbnail into an interface I am building to the repository.

My general flow is this.

- I get the document information

Code: Select all

  'http://myrepo.com/api/v4/documents/' . $document_id . '/';
- Extract the image

Code: Select all

  $image = $response['version_active']['pages_first']['image_url'] ?? '';
So the image url gives me something like
http://myrepo.com/api/v4/documents/3/ve ... /64/image/

I can enter this into a browser and will happily retrieve the file

However, using code

Code: Select all

            // This didn't work 
            //  $file_contents = file_get_contents($previewUrl);
            // ...so I tried this
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
            curl_setopt($ch, CURLOPT_URL, $previewUrl);
            $file_contents = curl_exec($ch);
            curl_close($ch);
I get the dreaded response.
{"detail":"Not found."}
How can I correctly retrieve the preview/thumbnail?

[SOLVED]: Getting document preview using the API

Posted: Wed Sep 21, 2022 7:29 am
by crafter
I solved this by providing user credentials to my request.

In my initial post I said this
I can enter this into a browser and will happily retrieve the file
However, using code
<snip>
...
</snip>
I get the dreaded response.
I was actually logged in the time hence the browser request passed.
Using a private browser window failed.

I fixed this by supplying user credentials to the request

Code: Select all

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
            curl_setopt($ch, CURLOPT_URL, $previewUrl);

            $headers = array();
            $headers[] = 'Accept: application/json';
            $headers[] = 'Authorization: Basic YWSSRtaW46QndLcFNSSSSSSaQnJURDI3MzN5';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            $file_contents = curl_exec($ch);
            curl_close($ch);
            return $file_contents;