本文整理汇总了PHP中JSON::convertToXML方法的典型用法代码示例。如果您正苦于以下问题:PHP JSON::convertToXML方法的具体用法?PHP JSON::convertToXML怎么用?PHP JSON::convertToXML使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JSON
的用法示例。
在下文中一共展示了JSON::convertToXML方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: transform
public function transform($data)
{
try {
$data = JSON::convertToXML($data);
} catch (Exception $ex) {
throw new TransformException($ex->getMessage(), array('message' => $ex->getMessage()));
}
return $data;
}
示例2: execute
public function execute(array &$param_pool = null)
{
$result = new XMLElement($this->dsParamROOTELEMENT);
// When DS is called out of the Frontend context, this will enable
// {$root} and {$workspace} parameters to be evaluated
if (empty($this->_env)) {
$this->_env['env']['pool'] = array('root' => URL, 'workspace' => WORKSPACE);
}
try {
require_once TOOLKIT . '/class.gateway.php';
require_once TOOLKIT . '/class.xsltprocess.php';
require_once CORE . '/class.cacheable.php';
$this->dsParamURL = $this->parseParamURL($this->dsParamURL);
if (isset($this->dsParamXPATH)) {
$this->dsParamXPATH = $this->__processParametersInString(stripslashes($this->dsParamXPATH), $this->_env);
}
// Builds a Default Stylesheet to transform the resulting XML with
$stylesheet = new XMLElement('xsl:stylesheet');
$stylesheet->setAttributeArray(array('version' => '1.0', 'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform'));
$output = new XMLElement('xsl:output');
$output->setAttributeArray(array('method' => 'xml', 'version' => '1.0', 'encoding' => 'utf-8', 'indent' => 'yes', 'omit-xml-declaration' => 'yes'));
$stylesheet->appendChild($output);
$template = new XMLElement('xsl:template');
$template->setAttribute('match', '/');
$instruction = new XMLElement('xsl:copy-of');
// Namespaces
if (isset($this->dsParamNAMESPACES) && is_array($this->dsParamNAMESPACES)) {
foreach ($this->dsParamNAMESPACES as $name => $uri) {
$instruction->setAttribute('xmlns' . ($name ? ":{$name}" : null), $uri);
}
}
// XPath
$instruction->setAttribute('select', $this->dsParamXPATH);
$template->appendChild($instruction);
$stylesheet->appendChild($template);
$stylesheet->setIncludeHeader(true);
$xsl = $stylesheet->generate(true);
// Check for an existing Cache for this Datasource
$cache_id = self::buildCacheID($this);
$cache = Symphony::ExtensionManager()->getCacheProvider('remotedatasource');
$cachedData = $cache->check($cache_id);
$writeToCache = null;
$isCacheValid = true;
$creation = DateTimeObj::get('c');
// Execute if the cache doesn't exist, or if it is old.
if (!is_array($cachedData) || empty($cachedData) || time() - $cachedData['creation'] > $this->dsParamCACHE * 60) {
if (Mutex::acquire($cache_id, $this->dsParamTIMEOUT, TMP)) {
$ch = new Gateway();
$ch->init($this->dsParamURL);
$ch->setopt('TIMEOUT', $this->dsParamTIMEOUT);
// Set the approtiate Accept: headers depending on the format of the URL.
if ($this->dsParamFORMAT == 'xml') {
$ch->setopt('HTTPHEADER', array('Accept: text/xml, */*'));
} elseif ($this->dsParamFORMAT == 'json') {
$ch->setopt('HTTPHEADER', array('Accept: application/json, */*'));
} elseif ($this->dsParamFORMAT == 'csv') {
$ch->setopt('HTTPHEADER', array('Accept: text/csv, */*'));
}
self::prepareGateway($ch);
$data = $ch->exec();
$info = $ch->getInfoLast();
Mutex::release($cache_id, TMP);
$data = trim($data);
$writeToCache = true;
// Handle any response that is not a 200, or the content type does not include XML, JSON, plain or text
if ((int) $info['http_code'] != 200 || !preg_match('/(xml|json|csv|plain|text)/i', $info['content_type'])) {
$writeToCache = false;
$result->setAttribute('valid', 'false');
// 28 is CURLE_OPERATION_TIMEOUTED
if ($info['curl_error'] == 28) {
$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'])));
}
return $result;
} else {
if (strlen($data) > 0) {
// Handle where there is `$data`
// If it's JSON, convert it to XML
if ($this->dsParamFORMAT == 'json') {
try {
require_once TOOLKIT . '/class.json.php';
$data = JSON::convertToXML($data);
} catch (Exception $ex) {
$writeToCache = false;
$errors = array(array('message' => $ex->getMessage()));
}
} elseif ($this->dsParamFORMAT == 'csv') {
try {
require_once EXTENSIONS . '/remote_datasource/lib/class.csv.php';
$data = CSV::convertToXML($data);
} catch (Exception $ex) {
$writeToCache = false;
$errors = array(array('message' => $ex->getMessage()));
}
} elseif ($this->dsParamFORMAT == 'txt') {
$txtElement = new XMLElement('entry');
$txtElement->setValue(General::wrapInCDATA($data));
$data = $txtElement->generate();
$txtElement = null;
//.........这里部分代码省略.........
示例3: createXML
/**
*
* Convert the $source string into a XML string
* @param string $source
* @return string
*/
public function createXML($source, $driver, $url, &$errorFlag)
{
// @see http://getsymphony.com/learn/api/2.3.2/toolkit/json/
return JSON::convertToXML($source);
}