xml parsing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello everyone. I got this script that sends XML data to United Parcel
Service (UPS) to receive rates.
XML data is sent to the server, and a response is sent back also in XML.

The problem is the parser, the data gets fed into it by url only, and I
would like to feed data to it via a variable that is filled by the curl
process of sending / receiving the XML.

Here is the parser and how I retrieve the data from it:



function startElement($parser, $name, $attrs=''){
  global $ary_path;
  array_push($ary_path, $name);
}

function endElement($parser, $name, $attrs=''){
  global $ary_path;
  array_pop($ary_path);
}

function characterData($parser, $data){
  global $ary_parsed_file, $ary_path, $int_starting_level;
  $str_trimmed_data = trim($data);
  if (!empty($str_trimmed_data)) {
    $str_array_define = '$ary_parsed_file';
      for ($i = $int_starting_level; $i < count($ary_path); $i++) {
        $str_array_define .= '[\'' . $ary_path[$i] . '\']';
      }
      $str_array_define .= " = '" . $str_trimmed_data . "';";
      eval($str_array_define);
       
      // DEBUG
      //echo "\n" . $str_array_define;
       
  } // if
}


$ary_path = array();
$ary_parsed_file = array();
$int_starting_level = 1;

$xml_file =
'http://www.domain.com/rates.php?zip='.$_SESSION["shipto_zip"].'&session
='.session_id()."&shiptype=".$_SESSION["shiptype"];

$type = 'UTF-8';

$xml_parser = xml_parser_create($type);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_set_element_handler($xml_parser, 'startElement','endElement');
xml_set_character_data_handler($xml_parser, 'characterData');

if (!($fp = fopen($xml_file, 'r'))) {
   die("Could not open $xml_file for parsing!\n");
}



while ($data = fread($fp, 1024000)) {
   if (!($data = utf8_encode($data))) {
       echo 'ERROR'."\n";
   }
   if (!xml_parse($xml_parser, $data, feof($fp))) {
       die(sprintf( "XML error: %s at line %d\n\n",
       xml_error_string(xml_get_error_code($xml_parser)),
       xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);

//print_r($ary_parsed_file);


#///// This loads up retrieves values into sessions for use in the
shopping cart.
$_SESSION["shipping_price"]=$ary_parsed_file['RATEDSHIPMENT']['TOTALCHAR
GES']['MONETARYVALUE'];
$_SESSION["shipping_weight"]=$ary_parsed_file['RATEDSHIPMENT']['BILLINGW
EIGHT']['WEIGHT'];




The file this script is connecting to (rates.php) is the actual script
and curl process that sends / receives the XML data to and from ups's
servers. The above code is just the parser.



Thank you in advance for any help.

-Wendell Frohwein

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux