Example #1
0
 /**
  * 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 PEAR_XMLParser
  * @return string|array
  */
 function retrieveData($url, $accept = false, $forcestring = false, $channel = false)
 {
     $cacheId = $this->getCacheId($url);
     if ($ret = $this->useLocalCache($url, $cacheId)) {
         return $ret;
     }
     $file = $trieddownload = false;
     if (!isset($this->_options['offline'])) {
         $trieddownload = true;
         $file = $this->downloadHttp($url, $cacheId ? $cacheId['lastChange'] : false, $accept, $channel);
     }
     if (PEAR::isError($file)) {
         if ($file->getCode() !== -9276) {
             return $file;
         }
         $trieddownload = false;
         $file = false;
         // use local copy if available on socket connect error
     }
     if (!$file) {
         $ret = $this->getCache($url);
         if (!PEAR::isError($ret) && $trieddownload) {
             // reset the age of the cache if the server says it was unmodified
             $result = $this->saveCache($url, $ret, null, true, $cacheId);
             if (PEAR::isError($result)) {
                 return PEAR::raiseErro($result->getMessage());
             }
         }
         return $ret;
     }
     if (is_array($file)) {
         $headers = $file[2];
         $lastmodified = $file[1];
         $content = $file[0];
     } else {
         $headers = array();
         $lastmodified = false;
         $content = $file;
     }
     if ($forcestring) {
         $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
         if (PEAR::isError($result)) {
             return PEAR::raiseErro($result->getMessage());
         }
         return $content;
     }
     if (isset($headers['content-type'])) {
         switch ($headers['content-type']) {
             case 'text/xml':
             case 'application/xml':
             case 'text/plain':
                 if ($headers['content-type'] === 'text/plain') {
                     $check = substr($content, 0, 5);
                     if ($check !== '<?xml') {
                         break;
                     }
                 }
                 $parser = new PEAR_XMLParser();
                 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
                 $err = $parser->parse($content);
                 PEAR::popErrorHandling();
                 if (PEAR::isError($err)) {
                     return PEAR::raiseError('Invalid xml downloaded from "' . $url . '": ' . $err->getMessage());
                 }
                 $content = $parser->getData();
             case 'text/html':
             default:
                 // use it as a string
         }
     } else {
         // assume XML
         $parser = new PEAR_XMLParser();
         $parser->parse($content);
         $content = $parser->getData();
     }
     $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
     if (PEAR::isError($result)) {
         return PEAR::raiseErro($result->getMessage());
     }
     return $content;
 }