/** * Retrieve a remote REST resource * * @param string full URL to this resource * @param array|false contents of the accept-encoding header * @param boolean if true, xml will be returned as a string, otherwise, xml will be * parsed using Pyrus\XMLParser * @param string|false if string, this will override the recieved content-type and can enforce a parser. * * @return string|array * * @throws Pyrus\REST\Exception If the xml cannot be parsed */ function retrieveData($url, $accept = false, $forcestring = false, $forceContentType = false) { $cacheId = $this->getCacheId($url); if ($ret = $this->useLocalCache($url, $cacheId)) { return $ret; } if (!isset($this->_options['offline'])) { $trieddownload = true; try { $file = $this->downloadHttp($url, $cacheId ? $cacheId['lastChange'] : false, $accept); } catch (\PEAR2\HTTP\Request\Exception $e) { $file = $trieddownload = false; } } else { $file = $trieddownload = false; } if (!$file) { $ret = $this->getCache($url); if ($trieddownload) { // reset the age of the cache if the server says it was unmodified $this->saveCache($url, $ret, null, true, $cacheId); } return $ret; } if (is_array($file)) { $headers = $file[2]; $lastmodified = $file[1]; $content = $file[0]; } else { $content = $file; $lastmodified = false; $headers = array(); } // handle HTTP-redirects if (isset($headers['location']) && $headers['location'] !== $url) { $content = $this->retrieveData($headers['location'], $accept, $forcestring, $forceContentType); } if ($forcestring) { $this->saveCache($url, $content, $lastmodified, false, $cacheId); return $content; } if (isset($headers['location']) && $headers['location'] !== $url) { return $content; } if (is_string($forceContentType)) { $ct = $forceContentType; } else { // Default to XML if no content-type is provided //TODO: Deal with text as well, look at PEAR 1.9/1.8 $ct = isset($headers['content-type']) ? $headers['content-type'] : 'text/xml'; } switch ($ct) { case 'text/xml': case 'application/xml': $parser = new XMLParser(); try { $content = $parser->parseString($content); $content = current($content); } catch (\Exception $e) { throw new REST\Exception('Invalid xml downloaded from "' . $url . '"', $e); } case 'text/html': default: // use it as a string } $this->saveCache($url, $content, $lastmodified, false, $cacheId); return $content; }