protected function setUp() { $libstub = $this->getMock('Zotero_Library', array('_request'), array('user', '475425', 'z_public_library')); $itemResponseBody = file_get_contents("../data/apiv1/individualItemJson"); $collectionItemsJsonBibResponseBody = file_get_contents("../data/apiv1/collectionItemsJsonBib"); $allcollectionsResponseBody = file_get_contents("../data/apiv1/allCollections"); $childCollectionsResponseBody = file_get_contents("../data/apiv1/childCollections"); $tagsResponseBody = file_get_contents("../data/apiv1/tagsresponse"); $collectionItemsResponse = libZotero_Http_Response::fromString($collectionItemsJsonBibResponseBody); $itemResponse = libZotero_Http_Response::fromString($itemResponseBody); $tagsResponse = libZotero_Http_Response::fromString($tagsResponseBody); $allcollectionsResponse = libZotero_Http_Response::fromString($allcollectionsResponseBody); $childCollectionsResponse = libZotero_Http_Response::fromString($childCollectionsResponseBody); //build urls with instance of library so it adds key if it will have it and stays consistent since we're not testing url building here $aparams = array('target' => 'items', 'key' => $libstub->_apiKey, 'collectionKey' => 'N7W92H48', 'content' => 'json,bib'); $fetchCollectionItemsUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $aparams = array('target' => 'item', 'key' => $libstub->_apiKey, 'content' => 'json', 'itemKey' => 'X42A7DEE'); $fetchItemUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $aparams = array('target' => 'collections', 'content' => 'json', 'limit' => '100', 'key' => $libstub->_apiKey); $fetchCollectionsUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $aparams = array('target' => 'collections', 'content' => 'json', 'collectionKey' => 'N7W92H48', 'key' => $libstub->_apiKey); $fetchChildCollectionsUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $aparams = array('target' => 'tags', 'content' => 'json', 'key' => $libstub->_apiKey); $fetchTagsUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $map = array(array($fetchCollectionItemsUrl1, 'GET', NULL, array(), array(), $collectionItemsResponse), array($fetchItemUrl1, 'GET', NULL, array(), array(), $itemResponse), array($fetchCollectionsUrl1, 'GET', NULL, array(), array(), $allcollectionsResponse), array($fetchChildCollectionsUrl1, 'GET', NULL, array(), array(), $childCollectionsResponse), array($fetchTagsUrl1, 'GET', NULL, array(), array(), $tagsResponse)); $libstub->expects($this->any())->method('_request')->will($this->returnValueMap($map)); $this->libstub = $libstub; }
protected function setUp() { $libstub = $this->getMock('Zotero_Library', array('_request'), array('user', '475425', 'z_public_library')); $aparams = array('target' => 'item', 'key' => $libstub->_apiKey, 'content' => 'json', 'itemKey' => 'X42A7DEE'); $fetchItemUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $itemResponseBody = file_get_contents("../data/apiv2/individualItemJson"); $itemResponse = libZotero_Http_Response::fromString($itemResponseBody); $aparams = array('target' => 'items', 'key' => $libstub->_apiKey, 'collectionKey' => 'N7W92H48', 'content' => 'json,bib'); $collectionItemsUrl = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $collectionItemsJsonBibResponseBody = file_get_contents("../data/apiv2/collectionItemsJsonBib"); $collectionItemsResponse = libZotero_Http_Response::fromString($collectionItemsJsonBibResponseBody); $aparams = array('target' => 'collections', 'content' => 'json', 'limit' => '100', 'key' => $libstub->_apiKey); $fetchCollectionsUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $allcollectionsResponseBody = file_get_contents("../data/apiv2/allCollections"); $allcollectionsResponse = libZotero_Http_Response::fromString($allcollectionsResponseBody); $aparams = array('target' => 'collections', 'limit' => 100, 'content' => 'json', 'collectionKey' => 'N7W92H48', 'key' => $libstub->_apiKey); $fetchChildCollectionsUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $childCollectionsResponseBody = file_get_contents("../data/apiv2/childCollections"); $childCollectionsResponse = libZotero_Http_Response::fromString($childCollectionsResponseBody); $aparams = array('target' => 'tags', 'limit' => 50, 'content' => 'json', 'key' => $libstub->_apiKey); $fetchTagsUrl1 = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $tagsResponseBody = file_get_contents("../data/apiv2/tagsresponse"); $tagsResponse = libZotero_Http_Response::fromString($tagsResponseBody); $aparams = array('limit' => 10, 'content' => 'json', 'target' => 'items', 'targetModifier' => 'top'); $fetchItemsTopUrl = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $fetchItemsTopResponseBody = file_get_contents("../data/apiv2/itemsTopResponse"); $fetchItemsTopResponse = libZotero_Http_Response::fromString($fetchItemsTopResponseBody); $aparams = array('limit' => 10, 'content' => 'json', 'target' => 'items', 'targetModifier' => 'top', 'collectionKey' => 'N7W92H48'); $fetchItemsTopInCollectionUrl = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $fetchItemsTopInCollectionResponseBody = file_get_contents("../data/apiv2/itemsTopInCollectionResponse"); $fetchItemsTopInCollectionResponse = libZotero_Http_Response::fromString($fetchItemsTopInCollectionResponseBody); $aparams = array('content' => 'json', 'target' => 'children', 'itemKey' => 'GUVBGARB'); $fetchChildItemsUrl = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $fetchChildItemsResponseBody = file_get_contents("../data/apiv2/childItems"); $fetchChildItemsResponse = libZotero_Http_Response::fromString($fetchChildItemsResponseBody); $aparams = array('content' => 'json', 'target' => 'collection', 'collectionKey' => 'N7W92H48'); $singleCollectionUrl = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $singleCollectionResponseBody = file_get_contents("../data/apiv2/singleCollection"); $singleCollectionResponse = libZotero_Http_Response::fromString($singleCollectionResponseBody); $map = array(array($collectionItemsUrl, 'GET', NULL, array(), array(), $collectionItemsResponse), array($fetchItemUrl1, 'GET', NULL, array(), array(), $itemResponse), array($fetchCollectionsUrl1, 'GET', NULL, array(), array(), $allcollectionsResponse), array($fetchChildCollectionsUrl1, 'GET', NULL, array(), array(), $childCollectionsResponse), array($fetchTagsUrl1, 'GET', NULL, array(), array(), $tagsResponse), array($fetchItemsTopUrl, 'GET', NULL, array(), array(), $fetchItemsTopResponse), array($fetchItemsTopInCollectionUrl, 'GET', NULL, array(), array(), $fetchItemsTopInCollectionResponse), array($fetchChildItemsUrl, 'GET', NULL, array(), array(), $fetchChildItemsResponse), array($singleCollectionUrl, 'GET', NULL, array(), array(), $singleCollectionResponse)); $libstub->expects($this->any())->method('_request')->will($this->returnValueMap($map)); /* echo "Stubbing Requests to GET URLS:\n"; foreach($map as $stubMapEntry){ echo $stubMapEntry[0] . "\n"; } echo "\n"; */ $this->libstub = $libstub; }
public function testTopItems() { $topItemsResponseBody = file_get_contents("../data/apiv2/topItemsBibCitationCoins"); $topItemsResponse = libZotero_Http_Response::fromString($topItemsResponseBody); $libstub = $this->getMock('Zotero_Library', array('_request'), array('user', '475425', 'z_public_library')); $aparams = array('limit' => 10, 'content' => 'bib,citation,coins', 'linkwrap' => 1, 'style' => 'chicago-fullnote-bibliography', 'target' => 'items', 'targetModifier' => 'top'); $topItemsUrl = $libstub->apiRequestUrl($aparams) . $libstub->apiQueryString($aparams); $map = array(array($topItemsUrl, 'GET', NULL, array(), array(), $topItemsResponse)); $libstub->expects($this->any())->method('_request')->will($this->returnValueMap($map)); $items = $libstub->fetchItemsTop(array('limit' => 10, 'content' => 'bib,citation,coins', 'linkwrap' => 1, 'style' => 'chicago-fullnote-bibliography')); $this->assertEquals(count($items), 10, "Got 10 item results"); $i = 0; foreach ($items as $item) { //test that subcontent blocks were extracted and are present $this->assertTrue(!empty($item->bibContent), "non-empty bibContent {$i}"); $this->assertTrue(!empty($item->subContents['bib']), "non-empty bib subContent {$i}"); $this->assertEquals(trim($item->bibContent), trim($item->subContents['bib']), 'bibContent and subContents[bib] are the same'); $this->assertTrue(!empty($item->subContents['citation']), "non-empty citation subContent {$i}"); $this->assertTrue(isset($item->subContents['coins']), "subContents['coins'] is set, even if empty {$i}"); //check that subcontent blocks are parseable $doc = new DOMDocument(); $this->assertTrue($doc->loadXml($item->subContents['bib']), "bibContent parsed into dom"); $this->assertTrue($doc->loadXml($item->subContents['citation']), "subContents['citation'] parsed into dom"); //TODO: should the decoding be necessary on the user side or should libZotero be decoding this before saving? //is all export translator data similarly encoded? if (!empty($item->subContents['coins'])) { $this->assertTrue($doc->loadXml(htmlspecialchars_decode($item->subContents['coins'])), "subContents['coins'] parsed into dom"); } $i++; } $firstItem = $items[0]; $doc = new DOMDocument(); $doc->loadXml($item->bibContent); $linkNodes = $doc->getElementsByTagName("a"); $this->assertTrue($linkNodes->length > 0, "anchor link found in bibContent"); }
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 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()) { libZoteroDebug("url being requested: " . $url . "\n\n"); $ch = curl_init(); $httpHeaders = array(); foreach ($headers as $key => $val) { $httpHeaders[] = "{$key}: {$val}"; } 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_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 3); $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 = apc_fetch($url, $success); if ($success) { $responseBody = $cachedResponse['responseBody']; $responseInfo = $cachedResponse['responseInfo']; $zresponse = libZotero_Http_Response::fromString($responseBody); $gotCached = true; } } if (!$gotCached) { $responseBody = curl_exec($ch); $responseInfo = curl_getinfo($ch); //libZoteroDebug( "{$method} url:" . $url . "\n"); //libZoteroDebug( "%%%%%" . $responseBody . "%%%%%\n\n"); $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); } $saveCached = array('responseBody' => $responseBody, 'responseInfo' => $responseInfo); if ($this->_cacheResponses) { apc_store($url, $saveCached, $this->_cachettl); } } $this->_lastResponse = $zresponse; return $zresponse; }
<?php /* * citeendpoint.php forwards requests from made from JS and forwards it * to the citation or translation server, then returns the response */ require_once './config.php'; //endpoints require_once '../library/libZotero/build/libZoteroSingle.php'; $queryString = $_SERVER['QUERY_STRING']; $requestUrl = $citationEndpoint . '?' . $queryString; $requestMethod = 'POST'; //raw body of the request $rawbody = @file_get_contents('php://input'); $ch = curl_init(); $httpHeaders = array(); $httpHeaders[] = "Content-Type: application/json"; curl_setopt($ch, CURLOPT_URL, $requestUrl); 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); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $rawbody); $responseBody = curl_exec($ch); $responseInfo = curl_getinfo($ch); $zresponse = libZotero_Http_Response::fromString($responseBody); echo $zresponse->getRawBody(); die;
/** * 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()) { 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']; 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_HTTPHEADER, array('Expect:')); 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 = apc_fetch($url, $success); if ($success) { $responseBody = $cachedResponse['responseBody']; $responseInfo = $cachedResponse['responseInfo']; $zresponse = libZotero_Http_Response::fromString($responseBody); $gotCached = true; } } if (!$gotCached) { $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 if ($this->_followRedirects) { while ($zresponse->isRedirect()) { $redirectedBody = $zresponse->getBody(); $zresponse = libZotero_Http_Response::fromString($redirectedBody); } } $saveCached = array('responseBody' => $responseBody, 'responseInfo' => $responseInfo); if ($this->_cacheResponses && !$zresponse->isError()) { apc_store($url, $saveCached, $this->_cachettl); } } $this->_lastResponse = $zresponse; return $zresponse; }