xmlurl=$xmlurl; if($this->GetUrl($this->xmlurl)) { $arrRSS = $this->Xml2Array($this->page); //echo "
";
//print_r($arrRSS);
return $arrRSS;
}
else
return "";
}
private function GetUrl( $url ) {
$retVal="";
$url_parsed = parse_url($url);
$scheme = $url_parsed["scheme"];
$host = $url_parsed["host"];
$port = isset($url_parsed["port"])?$url_parsed["port"]:"80";
$user = isset($url_parsed["user"])?$url_parsed["user"]:"";
$pass = isset($url_parsed["pass"])?$url_parsed["pass"]:"";
$path = $url_parsed["path"]?$url_parsed["path"]:"/";
$query = isset($url_parsed["query"])?$url_parsed["query"]:"";
$anchor = isset($url_parsed["fragment"])?$url_parsed["fragment"]:"";
if (!empty($host)){
if(!$fp = @fsockopen($host, $port, $errno, $errstr, 2)){
return false;
}
// attempt to open the socket
$path .= $query?"?$query":"";
$path .= $anchor?"$anchor":"";
// this is the request we send to the host
$out = "GET $path ".
"HTTP/1.0\r\n".
"Host: $host\r\n".
"Connection: Close\r\n".
"User-Agent: $this->userAgent\r\n";
if($user)
$out .= "Authorization: Basic ".
base64_encode("$user:$pass")."\r\n";
$out .= "\r\n";
fputs($fp, $out);
while (!feof($fp)) {
$retVal.=fgets($fp, 128);
}
fclose($fp);
$this->result=$retVal;
$this->headers=$this->parseHeaders(trim(substr($retVal,0,strpos($retVal,"\r\n\r\n"))));
$this->page=trim(stristr($retVal,"\r\n\r\n"))."\n";
if(isset($this->headers['Location'])){
$this->redirects++;
if($this->redirects<$this->maxRedirects){
$location=$this->headers['Location'];
$this->headers=array();
$this->result="";
$this->page="";
$this->getUrl($location);
}
}
}
return (!$retVal="");
}
private function parseHeaders($s){
$h=preg_split("/[\r\n]/",$s);
foreach($h as $i){
$i=trim($i);
if(strstr($i,":")){
list($k,$v)=explode(":",$i);
$hdr[strtolower($k)]=substr(stristr($i,":"),2);
}else{
if(strlen($i)>3)
$hdr[]=$i;
}
}
if(isset($hdr[0])){
$hdr['status']=$hdr[0];
unset($hdr[0]);
}
return $hdr;
}
private function Xml2Array($xml)
{
$objXml2Array=new xml2array($xml);
$arrResult=$objXml2Array->getResult();
//echo "";
// print_R($arrResult);
return $arrResult;
}
}
class xml2array
{
/**
* constructor
*/
function xml2array( $xml )
{
// check for file
if ( file_exists($xml) )
$xml = file_get_contents( $xml );
// check for string, open in dom
if ( is_string($xml) )
{
$xml = domxml_open_mem( $xml );
$this->root_element = $xml->document_element();
}
// check for dom-creation,
if ( is_object( $xml ) && $xml->node_type() == XML_DOCUMENT_NODE )
{
$this->root_element = $xml->document_element();
//$this->xml_string = $xml->dump_mem(true);
return TRUE;
}
if ( is_object( $xml ) && $xml->node_type() == XML_ELEMENT_NODE )
{
$this->root_element = $xml;
return TRUE;
}
return FALSE;
}
/**
* recursive function to walk through dom and create array
*/
function _recNode2Array( $domnode )
{
if ( $domnode->node_type() == XML_ELEMENT_NODE )
{
$childs = $domnode->child_nodes();
foreach($childs as $child)
{
if ($child->node_type() == XML_ELEMENT_NODE)
{
$subnode = false;
$prefix = ( $child->prefix() ) ? $child->prefix().':' : '';
// try to check for multisubnodes
foreach ($childs as $testnode)
if ( is_object($testnode) )
if ($child->node_name() == $testnode->node_name() && $child != $testnode)
$subnode = true;
if ( is_array($result[ $prefix.$child->node_name() ]) )
$subnode = true;
if ($subnode == true)
$result[ $prefix.$child->node_name() ][] = $this->_recNode2Array($child);
else
$result[ $prefix.$child->node_name() ] = $this->_recNode2Array($child);
}
}
if ( !is_array($result) ){
// correct encoding from utf-8 to locale
// NEEDS to be updated to correct in both ways!
$result['#text'] = html_entity_decode(htmlentities($domnode->get_content(), ENT_COMPAT, 'UTF-8'), ENT_COMPAT,'ISO-8859-15');
}
if ( $domnode->has_attributes() )
foreach ( $domnode->attributes() as $attrib )
{
$prefix = ( $attrib->prefix() ) ? $attrib->prefix().':' : '';
$result["@".$prefix.$attrib->name()] = $attrib->value();
}
return $result;
}
}
/**
* caller func to get an array out of dom
*/
function getResult()
{
if ( $resultDomNode = $this->root_element )
{
$array_result[ $resultDomNode->tagname() ] = $this->_recNode2Array( $resultDomNode );
return $array_result;
} else
return false;
}
function getEncoding()
{
preg_match("~\<\?xml.*encoding=[\"\'](.*)[\"\'].*\?\>~i",$this->xml_string,$matches);
return ($matches[1])?$matches[1]:"";
}
function getNamespaces()
{
preg_match_all("~[[:space:]]xmlns:([[:alnum:]]*)=[\"\'](.*?)[\"\']~i",$this->xml_string,$matches,PREG_SET_ORDER);
foreach( $matches as $match )
$result[ $match[1] ] = $match[2];
return $result;
}
}
?>