public function __construct($entryNode) { if (!$entryNode) { libZoteroDebug("no entryNode in tag constructor\n"); return; } elseif (is_string($entryNode)) { libZoteroDebug("entryNode is string in tag constructor\n"); $xml = $entryNode; $doc = new DOMDocument(); libZoteroDebug($xml); $doc->loadXml($xml); $entryNode = $doc->getElementsByTagName('entry')->item(0); } parent::__construct($entryNode); $this->name = $this->title; if (!$entryNode) { libZoteroDebug("second no entryNode in tag constructor\n"); return; } $numItems = $entryNode->getElementsByTagNameNS('http://zotero.org/ns/api', "numItems")->item(0); if ($numItems) { $this->numItems = (int) $numItems->nodeValue; } $tagElements = $entryNode->getElementsByTagName("tag"); $tagElement = $tagElements->item(0); $contentNode = $entryNode->getElementsByTagName('content')->item(0); if ($contentNode) { $contentType = $contentNode->getAttribute('type'); if ($contentType == 'application/json') { $this->pristine = json_decode($contentNode->nodeValue, true); $this->apiObject = json_decode($contentNode->nodeValue, true); } elseif ($contentType == 'xhtml') { //$this->parseXhtmlContent($contentNode); } } }
public static function utilRequest($url, $method = "GET", $body = NULL, $headers = array(), $basicauth = array()) { libZoteroDebug("url being requested: " . $url . "\n\n"); $ch = curl_init(); $httpHeaders = array(); //set api version - allowed to be overridden by passed in value if (!isset($headers['Zotero-API-Version'])) { $headers['Zotero-API-Version'] = ZOTERO_API_VERSION; } foreach ($headers as $key => $val) { $httpHeaders[] = "{$key}: {$val}"; } //disable Expect header $httpHeaders[] = 'Expect:'; if (!empty($basicauth)) { $passString = $basicauth['username'] . ':' . $basicauth['password']; /* echo $passString; curl_setopt($ch, CURLOPT_USERPWD, $passString); curl_setopt($ch, CURLOPT_FORBID_REUSE, true); */ $authHeader = 'Basic ' . base64_encode($passString); $httpHeaders[] = "Authorization: {$authHeader}"; } else { $passString = ''; curl_setopt($ch, CURLOPT_USERPWD, $passString); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($ch, CURLOPT_MAXREDIRS, 3); //FOLLOW LOCATION HEADERS curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $umethod = strtoupper($method); switch ($umethod) { case "GET": curl_setopt($ch, CURLOPT_HTTPGET, true); break; case "POST": curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); break; case "PUT": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); break; case "DELETE": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); break; } $responseBody = curl_exec($ch); $responseInfo = curl_getinfo($ch); $zresponse = libZotero_Http_Response::fromString($responseBody); //Zend Response does not parse out the multiple sets of headers returned when curl automatically follows //a redirect and the new headers are left in the body. Zend_Http_Client gets around this by manually //handling redirects. That may end up being a better solution, but for now we'll just re-read responses //until a non-redirect is read while ($zresponse->isRedirect()) { $redirectedBody = $zresponse->getBody(); $zresponse = libZotero_Http_Response::fromString($redirectedBody); } curl_close($ch); return $zresponse; }
/** * Make a single request for Zotero tags in this library defined by the passed parameters * * @param array $params list of parameters defining the request * @return array $tags */ public function fetchTags($params) { $aparams = array_merge(array('target' => 'tags', 'content' => 'json', 'limit' => 50), $params); $reqUrl = $this->apiRequestUrl($aparams) . $this->apiQueryString($aparams); $response = $this->_request($reqUrl, 'GET'); if ($response->isError()) { libZoteroDebug($response->getMessage() . "\n"); libZoteroDebug($response->getBody()); return false; } $doc = new DOMDocument(); $doc->loadXml($response->getBody()); $feed = new Zotero_Feed($doc); $entries = $doc->getElementsByTagName('entry'); $tags = array(); foreach ($entries as $entry) { $tag = new Zotero_Tag($entry); $tags[] = $tag; } return $tags; }
/** * Make http request to zotero api * * @param string $url target api url * @param string $method http method GET|POST|PUT|DELETE * @param string $body request body if write * @param array $headers headers to set on request * @return HTTP_Response */ public function request($url, $method = "GET", $body = NULL, $headers = array(), $basicauth = array()) { if (is_array($url)) { $url = Url::apiRequestString($url); } libZoteroDebug("url being requested: " . $url . "\n\n"); $ch = curl_init(); $httpHeaders = array(); //set api version - allowed to be overridden by passed in value if (!isset($headers['Zotero-API-Version'])) { $headers['Zotero-API-Version'] = ZOTERO_API_VERSION; } if (!isset($headers['User-Agent'])) { $headers['User-Agent'] = 'LibZotero-php'; } foreach ($headers as $key => $val) { $httpHeaders[] = "{$key}: {$val}"; } //disable Expect header $httpHeaders[] = 'Expect:'; if (!empty($basicauth)) { $passString = $basicauth['username'] . ':' . $basicauth['password']; curl_setopt($ch, CURLOPT_USERPWD, $passString); curl_setopt($ch, CURLOPT_FORBID_REUSE, true); } else { curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); curl_setopt($ch, CURLOPT_MAXREDIRS, 3); if ($this->followRedirects) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); } else { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); } $umethod = strtoupper($method); switch ($umethod) { case "GET": curl_setopt($ch, CURLOPT_HTTPGET, true); break; case "POST": curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); break; case "PUT": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); break; case "DELETE": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); break; } $gotCached = false; if ($this->cacheResponses && $umethod == 'GET') { $cachedResponse = $this->cache->fetch($url, $success); if ($success) { $responseBody = $cachedResponse['responseBody']; $responseInfo = $cachedResponse['responseInfo']; $zresponse = HttpResponse::fromString($responseBody); $gotCached = true; } } if (!$gotCached) { $responseBody = curl_exec($ch); $responseInfo = curl_getinfo($ch); $zresponse = HttpResponse::fromString($responseBody); //Zend Response does not parse out the multiple sets of headers returned when curl automatically follows //a redirect and the new headers are left in the body. Zend_Http_Client gets around this by manually //handling redirects. That may end up being a better solution, but for now we'll just re-read responses //until a non-redirect is read if ($this->followRedirects) { while ($zresponse->isRedirect()) { $redirectedBody = $zresponse->getBody(); $zresponse = HttpResponse::fromString($redirectedBody); } } $saveCached = array('responseBody' => $responseBody, 'responseInfo' => $responseInfo); if ($this->cacheResponses && !$zresponse->isError()) { $this->cache->store($url, $saveCached, $this->cachettl); } } $this->_lastResponse = $zresponse; return $zresponse; }
/** * Get a template for a new item of a certain type * * @param string $itemType type of item the template is for * @return Item */ public function getTemplateItem($itemType, $linkMode = null) { $newItem = new Item(null, $this); $aparams = array('target' => 'itemTemplate', 'itemType' => $itemType); if ($linkMode) { $aparams['linkMode'] = $linkMode; } $response = $this->request($aparams); if ($response->isError()) { throw new Exception("API error retrieving item template - {$response->getStatus()} : {$response->getRawBody()}"); } libZoteroDebug($response->getRawBody()); $itemTemplate = json_decode($response->getRawBody(), true); $newItem->initItemFromTemplate($itemTemplate); return $newItem; }
/** * Get groups a user belongs to * * @param string $userID * @return array $groups */ public function fetchGroups($userID = '') { if ($userID == '') { $userID = $this->libraryID; } $aparams = array('target' => 'userGroups', 'userID' => $userID, 'content' => 'json', 'order' => 'title'); $reqUrl = $this->apiRequestString($aparams); $response = $this->_request($reqUrl, 'GET'); if ($response->isError()) { libZoteroDebug($response->getStatus()); libZoteroDebug($response->getBody()); return false; } $entries = Zotero_Lib_Utils::getEntryNodes($response->getRawBody()); $groups = array(); foreach ($entries as $entry) { $group = new Zotero_Group($entry); $groups[] = $group; } return $groups; }