本文整理汇总了PHP中XMLParser::parseString方法的典型用法代码示例。如果您正苦于以下问题:PHP XMLParser::parseString方法的具体用法?PHP XMLParser::parseString怎么用?PHP XMLParser::parseString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XMLParser
的用法示例。
在下文中一共展示了XMLParser::parseString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: retrieveData
/**
* 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;
}