public static function convert($json, $return = 'document') { self::$dom = new DOMDocument('1.0', 'utf-8'); self::$dom->formatOutput = TRUE; // remove callback functions from JSONP if (preg_match('/(\\{|\\[).*(\\}|\\])/s', $json, $matches)) { $json = $matches[0]; } else { throw new Exception('JSON not formatted correctly'); } $data = json_decode($json); $data_element = self::_process($data, self::$dom->createElement('data')); self::$dom->appendChild($data_element); switch ($return) { case 'fragment': return self::$dom->saveXML($data_element); case 'object': return self::$dom; default: return self::$dom->saveXML(); } }
if (!is_array($cachedData) || empty($cachedData) || time() - $cachedData['creation'] > $this->dsParamCACHE * 60) { if (Mutex::acquire($cache_id, $timeout, TMP)) { $start = precision_timer(); $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $this->dsParamURL); $ch->setopt('TIMEOUT', $timeout); $xml = $ch->exec(); $writeToCache = true; $end = precision_timer('STOP', $start); $info = $ch->getInfoLast(); Mutex::release($cache_id, TMP); // check to see if it is JSON and convert it to XML if (preg_match('/(json|javascript)/i', $info['content_type'])) { require_once EXTENSIONS . '/dynamic_json/lib/class.json_to_xml.php'; $xml = Json_to_xml::convert($xml); } $xml = trim($xml); if ((int) $info['http_code'] != 200 || !preg_match('/(xml|plain|text|json|javascript)/i', $info['content_type'])) { $writeToCache = false; if (is_array($cachedData) && !empty($cachedData)) { $xml = trim($cachedData['data']); $valid = false; $creation = DateTimeObj::get('c', $cachedData['creation']); } else { $result = new XMLElement($this->dsParamROOTELEMENT); $result->setAttribute('valid', 'false'); if ($end > $timeout) { $result->appendChild(new XMLElement('error', sprintf('Request timed out. %d second limit reached.', $timeout))); } else { $result->appendChild(new XMLElement('error', sprintf('Status code %d was returned. Content-type: %s', $info['http_code'], $info['content_type'])));