Attachment URL - API via PHP CRON

41 views
Skip to first unread message

Dean Hodges

unread,
Sep 29, 2021, 2:31:52 AM9/29/21
to zotero-dev
Hi All,

I am using the Zotero API to generate a search results page, in PHP. With the returned data I am outputting to the page. The search is for reports and thus have uploaded attachments to a record. Some via "linkMode->imported_url" or "linkMode->imported_file".

How do i create a link to the physical file uploaded? The URLs in "links" all return "Not Found" and the web viewer to Zotero yields no clues.

Dan Stillman

unread,
Sep 29, 2021, 2:35:30 AM9/29/21
to zoter...@googlegroups.com
You don't, to non-Zotero users. Zotero libraries don't allow public file
sharing, so the attachment links included in API responses require
authentication and produce temporary URLs with short TTLs.

Your PHP code can download the files, store a local cache, and
distribute those to your users as you see fit.

Dean Hodges

unread,
Sep 29, 2021, 2:45:31 AM9/29/21
to zotero-dev
Ahh ok, how can I access to download the file? I am not using the SDK, just a CURL lookup

Dean Hodges

unread,
Sep 29, 2021, 3:05:16 AM9/29/21
to zotero-dev
Apologies if i am missing something really obvious here

Dan Stillman

unread,
Sep 29, 2021, 3:11:38 AM9/29/21
to zoter...@googlegroups.com
On 9/29/21 2:45 AM, Dean Hodges wrote:
> Ahh ok, how can I access to download the file? I am not using the SDK,
> just a CURL lookup

It might not actually be documented, but there's an 'enclosure' link in
the response for attachment items with files stored online. It defaults
to a /file/view URL, which is meant for temporary viewing online, but
you can strip that and use just /file for a temporary S3 download URL.

Dean Hodges

unread,
Sep 29, 2021, 3:28:32 AM9/29/21
to zoter...@googlegroups.com
Hi Dan,

I tried that URL path aswell and it didnt work unfortunately. I get the same error "Not Found"

--
You received this message because you are subscribed to a topic in the Google Groups "zotero-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/zotero-dev/cQFMZbD2hi0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to zotero-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/zotero-dev/0100017c3064fa78-4ffc83ce-40ba-49c3-9a30-97fa3067adb0-000000%40email.amazonses.com.

Dean Hodges

unread,
Sep 29, 2021, 3:29:17 AM9/29/21
to zoter...@googlegroups.com
Basically I went through the returned API data to find a URL that works and none of them do but in the webviewer it works ok so i know the file is correct

Dan Stillman

unread,
Sep 29, 2021, 3:35:36 AM9/29/21
to zoter...@googlegroups.com
On 9/29/21 3:28 AM, Dean Hodges wrote:
> I tried that URL path aswell and it didnt work unfortunately. I get
> the same error "Not Found"

What's the URL you're trying?

Dean Hodges

unread,
Sep 29, 2021, 3:46:43 AM9/29/21
to zotero-dev

Dean Hodges

unread,
Sep 29, 2021, 3:49:20 AM9/29/21
to zotero-dev
Which is part of:

["enclosure"]=> object(stdClass)#1696 (4) { ["type"]=> string(15) "application/pdf" ["href"]=> string(62) "https://api.zotero.org/groups/2563422/items/ZC8U38Q6/file/view" ["title"]=> string(27) "Money Flows_Full report.pdf" ["length"]=> int(11050382) }

Dan Stillman

unread,
Sep 29, 2021, 3:52:44 AM9/29/21
to zoter...@googlegroups.com
On 9/29/21 3:46 AM, Dean Hodges wrote:
> https://api.zotero.org/groups/2563422/items/ZC8U38Q6/file/

First, while it will work either way, don't add the trailing '/'.

But that's a perfectly valid URL for an attachment with a file available
online. I'd guess that you're just making an unauthenticated request
without an API key.

Dean Hodges

unread,
Sep 29, 2021, 4:03:21 AM9/29/21
to zotero-dev
Ahh ok, I get it now. Just completed a test in postman and sent the bearer token and API key and the PDF was returned.
I will give it a go. 
Thank you :-)

Dean Hodges

unread,
Oct 14, 2021, 4:12:22 AM10/14/21
to zotero-dev
Hi Dan,

I am still having issues I am afraid. Turns out that the query was pulling through the attachment and not a report with a list of attachments. For example, I have one entry which type is report. And on this entry are 2 attachments but these do not appear in the API return for me to link directly to the files.

Any idea?

Dean Hodges

unread,
Oct 14, 2021, 4:14:27 AM10/14/21
to zotero-dev
["attachment"]=>
      object(stdClass)#8 (4) {
        ["href"]=>
        ["type"]=>
        string(16) "application/json"
        ["attachmentType"]=>
        string(15) "application/pdf"
        ["attachmentSize"]=>
        int(249215)
      }

Not sure what or which this is returning but there are more than one file for this entry. I thought it might have had a nested array for each attachment.

Dan Stillman

unread,
Oct 14, 2021, 4:44:16 AM10/14/21
to zoter...@googlegroups.com
On 10/14/21 4:14 AM, Dean Hodges wrote:
> ["attachment"]=>
>       object(stdClass)#8 (4) {
>         ["href"]=>
>         string(52) "https://api.zotero.org/groups/2563422/items/3XAWAZ97"
>         ["type"]=>
>         string(16) "application/json"
>         ["attachmentType"]=>
>         string(15) "application/pdf"
>         ["attachmentSize"]=>
>         int(249215)
>       }
>
> Not sure what or which this is returning but there are more than one
> file for this entry. I thought it might have had a nested array for
> each attachment.

The 'attachment' property is the "best" attachment — equivalent to
double-clicking an item on the desktop or web library.

The documentation explains how to get all child items.

Dean Hodges

unread,
Oct 14, 2021, 4:58:34 AM10/14/21
to zotero-dev
Hi Dan,

The attachment URL isnt going to work as there are multiple attachments. Can you send a link to the API for getting child items for myself and anyone looking at this thread.

Thanks

Dean Hodges

unread,
Oct 14, 2021, 5:01:40 AM10/14/21
to zotero-dev
Reply to my own question (and for anyone else having the same issue)...

It would seem that Zotero stores attachments as 'records'. So i had to do the first request with 'recrodType = report' or exclude 'recordType = attachment'. 
To get a nested attachment you need to use the Key and append '/children' to the request URL

See "Children"





Dan Stillman

unread,
Oct 14, 2021, 5:15:13 AM10/14/21
to zoter...@googlegroups.com
On 10/14/21 5:01 AM, Dean Hodges wrote:
> It would seem that Zotero stores attachments as 'records'. So i had to
> do the first request with 'recrodType = report' or exclude 'recordType
> = attachment'.

Not sure what you're referring to here, but we don't call anything a
"record" or use "recordType" in any way.
Reply all
Reply to author
Forward
0 new messages