0

我知道这可能问得太多了,但是我一直在阅读有关 xml 解析的内容,但我就是不明白!在我完成编程之前我没有意识到我的客户有 PHP 4(一个很好的教训),但我需要这个在 php4 中工作:

$post_string= 'type=xml&cid=55505&minorRev=14&apiKey=4sr8d8bsn75tpcuja6ypx5g3&locale='.$userLocale.'&currencyCode='.$userCurr.'&customerIpAddress='.$userIp.'&customerUserAgent='.$userAgent.'&xml=<HotelListRequest><destinationId>7EEF0188-E4DC-4B45-B4A7-57E3DF950E1F</destinationId><supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance><numberOfResults>200</numberOfResults></HotelListRequest>';
//Relative path to the file with $_POST parsing
$path = "http://api.ean.com/ean-services/rs/hotel/v3/list"; 
$ch = curl_init($path); 
$fp = fopen('xml/data.xml','w');
//Send the data to the file
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml')); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILE, $fp);
$val = curl_exec($ch);
curl_close($ch);//Close curl session
fclose($fp); //Close file overwrite
$data = simplexml_load_file('xml/data.xml');

我简直被难住了。我只使用过这种格式来读取 xml 文档。任何人都可以将其转换为在 php4 上工作吗?我再次知道这是很多问题,但任何帮助都会非常感激。先感谢您。

4

1 回答 1

2

您应该向您的客户说明,自2008 年8 月 8以来PHP 4根本没有任何支持!

也就是说,阅读这个已经解决了您的问题的问题。

(来自参考答案)

$xml = ...; // Get your XML data
$xml_parser = xml_parser_create();

// _start_element and _end_element are two functions that determine what
// to do when opening and closing tags are found
xml_set_element_handler($xml_parser, "_start_element", "_end_element");

// How to handle each char (stripping whitespace if needs be, etc
xml_set_character_data_handler($xml_parser, "_character_data");  

xml_parse($xml_parser, $xml);

如果您的客户真的想坚持使用不受支持的 PHP 版本(或无法迁移到 PHP 5),那么应该为此承担费用;维护遗留代码通常需要更多的努力。

于 2012-10-13T18:37:29.733 回答