Using PHP to query Zotero’s REST API

Today I tried to query Zotero’s REST API with PHP to retrieve a list of my bibliography items for further processing. The basics are pretty easy setup using phpZotero and following this guide: Mashup Guide :: Zotero REST API: early developments.

However, I ran into some problems as the user id could be read by getUserId() but getUserItems() returned an empty item list. I found the solution after analyzing the request URLs: the user id is read using HTTP while all the other functions use HTTPS. So I simply added these two lines to httpRequest() to avoid checking the server certificate: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

Furthermore, the name of the parameter for the API key is not apiKey but key. So the URL pointing to my bibliography items has to be https://api.zotero.org/users/{USERID}/items/?key={APIKEY} (you don’t need username). Although the API key is passed to phpZotero’s constructor, the class doesn’t use it in any of its HTTP calls, so I added this line to zoteroRequest():
$request .= '?key=' . $this->apiKey;

After theses small changes my items could be read successfully.

Über Stefan

Polyglot Clean Code Developer

2 Kommentare

  1. Would you mind publishing your php-files? Where would I find the output of your library?

    Best wishes

  2. @Klaus: See my Zotero Plugin for DokuWiki for an example on how to use the Zotero API.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax