Ver Mensaje Individual
  #5 (permalink)  
Antiguo 03/06/2008, 10:35
yazo
 
Fecha de Ingreso: abril-2003
Mensajes: 363
Antigüedad: 21 años, 10 meses
Puntos: 3
Respuesta: ¿Como puedo tratar este XML?

Hola,

He conseguido algo pero parcialmente, ya que funciona en mi servidor de pruebas, pero en internet no funciona.
El código es el siguiente:

index.php
Código PHP:
/**
 * xml2array() will convert the given XML text to an array in the XML structure.
 * Link: http://www.bin-co.com/php/scripts/xml2array/
 * Arguments : $contents - The XML text
 *                $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
 * Return: The parsed XML in an array form.
 */
function xml2array($contents, $get_attributes=1) {
    if(!$contents) return array();

    if(!function_exists('xml_parser_create')) {
        //print "'xml_parser_create()' function not found!";
        return array();
    }
    //Get the XML parser of PHP - PHP must have this module for the parser to work
    $parser = xml_parser_create();
    xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
    xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
    xml_parse_into_struct( $parser, $contents, $xml_values );
    xml_parser_free( $parser );

    if(!$xml_values) return;//Hmm...

    //Initializations
    $xml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$xml_array;

    //Go through the tags.
    foreach($xml_values as $data) {
        unset($attributes,$value);//Remove existing values, or there will be trouble

        //This command will extract these variables into the foreach scope
        // tag(string), type(string), level(int), attributes(array).
        extract($data);//We could use the array by itself, but this cooler.

        $result = '';
        if($get_attributes) {//The second argument of the function decides this.
            $result = array();
            if(isset($value)) $result['value'] = $value;

            //Set the attributes too.
            if(isset($attributes)) {
                foreach($attributes as $attr => $val) {
                    if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
                    /**  :TODO: should we change the key name to '_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
                }
            }
        } elseif(isset($value)) {
            $result = $value;
        }

        //See tag status and do the needed.
        if($type == "open") {//The starting of the tag '<tag>'
            $parent[$level-1] = &$current;

            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                $current[$tag] = $result;
                $current = &$current[$tag];

            } else { //There was another element with the same tag name
                if(isset($current[$tag][0])) {
                    array_push($current[$tag], $result);
                } else {
                    $current[$tag] = array($current[$tag],$result);
                }
                $last = count($current[$tag]) - 1;
                $current = &$current[$tag][$last];
            }

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
            //See if the key is already taken.
            if(!isset($current[$tag])) { //New Key
                $current[$tag] = $result;

            } else { //If taken, put all things inside a list(array)
                if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
                        or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
                    array_push($current[$tag],$result); // ...push the new element into that array.
                } else { //If it is not an array...
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                }
            }

        } elseif($type == 'close') { //End of tag '</tag>'
            $current = &$parent[$level-1];
        }
    }

    return($xml_array);
}



require_once('post_xml.php');

$xml ="<?xml version='1.0' encoding='ISO-8859-1'?>
      <ConsultarTiposHabYPaquetes auth='xml:reservasxml:arcangel:800:08:'>
            <FechaLlegada>10/6/08</FechaLlegada>
            <FechaSalida>20/6/08</FechaSalida>
      </ConsultarTiposHabYPaquetes>";
      
$url ='http://xx.xx.xx.xx';
$port = 606;

$response = xml_post($xml, $url, $port);

//imprimimos la respuesta
$resultado = xml2array($response);

function echo_array($array,$return_me=false){
    if(is_array($array) == false){
        $return = "The provided variable is not an array.";
    }else{
        foreach($array as $name=>$value){
            if(is_array($value)){
                $return .= "";
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
                $return .= echo_array($value,true);
                $return .= "</div>}";
                $return .= "\n\n";
            }else{
                if(is_string($value)){
                    $value = "\"$value\"";
                }
                $return .= "['<b>$name</b>'] = $value\n\n";
            }
        }
    }
    if($return_me == true){
        return $return;
    }else{
        echo "<pre>".$return."</pre>";
    }
}

echo_array($resultado);
post_xml.php
Código PHP:
// open a http channel, transmit data and return received buffer
    
function xml_post($post_xml$url$port)
    {
        
$user_agent $_SERVER['HTTP_USER_AGENT'];

        
$ch curl_init();    // initialize curl handle
        
curl_setopt($chCURLOPT_URL$url); // set url to post to
        
curl_setopt($chCURLOPT_FAILONERROR1);              // Fail on errors
        
curl_setopt($chCURLOPT_FOLLOWLOCATION1);    // allow redirects
        
curl_setopt($chCURLOPT_RETURNTRANSFER,1); // return into a variable
        
curl_setopt($chCURLOPT_PORT$port);            //Set the port number
        
curl_setopt($chCURLOPT_TIMEOUT15); // times out after 15s
        
curl_setopt($chCURLOPT_POSTFIELDS$post_xml); // add POST fields
        
curl_setopt($chCURLOPT_USERAGENT$user_agent);

        if(
$port==443)
        {
            
curl_setopt($chCURLOPT_SSL_VERIFYHOST,  2);
            
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
        }

        
$data curl_exec($ch);

        
curl_close($ch);

        return 
$data;
    } 
Creo que falla por el curl, pero nose exactamente en que, porque la version es la misma en internet que en mi servidor.
¿Me podeis ayudar?

Muchas Gracias