Bug #74201 [Opn->Wfx]: Strange SimpleXMLIterator Behaviour
| From: | requinix@php.net | Date: | Fri, 03 Mar 2017 09:00:49 +0000 |
| Subject: | Bug #74201 [Opn->Wfx]: Strange SimpleXMLIterator Behaviour | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-207674@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74201&edit=1
ID: 74201
Updated by: requinix@php.net
Reported by: dac514 at hotmail dot com
Summary: Strange SimpleXMLIterator Behaviour
-Status: Open
+Status: Wont fix
Type: Bug
Package: SimpleXML related
Operating System: CentOS
-PHP Version: Irrelevant
+PHP Version: 5.4.35
Block user comment: N
Private report: N
New Comment:
Seems it was fixed in 5.5.27, which came with the fix for
https://bugs.php.net/bug.php?id=66084
https://github.com/php/php-src/commit/5683b6fa3962f483a407ca17d49397b203ed4145
and other similar bugs that were caused by whitespace issues. The change was fairly simple (within
sxe_get_prop_hash) so you should be able to apply that to the 5.4 sources and build a custom patched
version for yourself.
Previous Comments:
------------------------------------------------------------------------
[2017-03-03 02:20:34] dac514 at hotmail dot com
Description:
------------
We upgraded a legacy CentOS box from PHP 5.3.X to 5.4.35 and our XML code started to break.
I understand that these versions are not supported but for this particular project this is the best
we can do. The weird thing is that this code works in 5.3, 5.5, 5.6, 7.0 ... But not 5.4.35 on
CentOS. Running the attached test script on that particular version will echo "> YOU HAVE
FAILED... "
I'm looking for some help, duplicate bugs that explain what is going on, or workarounds.
Thank you for your time.
Test script:
---------------
<?php
// ------------------------------------------------------------------------------------------------
// Functions
// ------------------------------------------------------------------------------------------------
function _iterator2array($iterator)
{
$array = array();
for ($iterator->rewind(); $iterator->valid(); $iterator->next())
{
if (!array_key_exists($iterator->key(), $array)) $array[$iterator->key()] = array();
if ($iterator->hasChildren()) $array[$iterator->key()][] =
_iterator2array($iterator->current());
else
{
$attributes = (array)$iterator->current();
$array[$iterator->key()][] = reset($attributes);
}
}
return $array;
}
function _xml2array($xml)
{
// Strip the XML header if there is one and add a parent container
if (preg_match('/<\?xml.*\?>/', $xml)) $xml = '<?xml
version="1.0"
encoding="UTF-8"?><root>'.preg_replace('/<\?xml.*\?>/',
'', $xml).'</root>';
$iterator = new SimpleXMLIterator($xml, NULL);
return _iterator2array($iterator);
}
// ------------------------------------------------------------------------------------------------
// Input
// ------------------------------------------------------------------------------------------------
$xml = '<?xml version="1.0" encoding="UTF-8"?>\n<shipment-info
xmlns="http://www.canadapost.ca/ws/shipment"><shipment-id>178011488481085223</shipment-id><shipment-status>created</shipment-status><tracking-pin>123456789012</tracking-pin><links><link
rel="self" href="https://ct.soa-gw.canadapost.ca/rs/0007174985/0007174985/shipment/178011488481085223"
media-type="application/vnd.cpc.shipment-v2+xml"/><link rel="details"
href="https://ct.soa-gw.canadapost.ca/rs/0007174985/0007174985/shipment/178011488481085223/details"
media-type="application/vnd.cpc.shipment-v2+xml"/><link rel="group"
href="https://ct.soa-gw.canadapost.ca/rs/0007174985/0007174985/shipment?groupId=ssense"
media-type="application/vnd.cpc.shipment-v2+xml"/><link rel="price"
href="https://ct.soa-gw.canadapost.ca/rs/0007174985/0007174985/shipment/178011488481085223/price"
media-type="application/vnd.cpc.shipment-v2+xml"/><link rel="label"
href="https://ct.soa-gw.canadapost.ca/rs/artifact/4096ae613c3f147c/1000035259!
3/0" media-type="application/pdf"
index="0"/></links></shipment-info>';
$response = _xml2array($xml);
// ------------------------------------------------------------------------------------------------
// Test
// ------------------------------------------------------------------------------------------------
if (empty($response['shipment-info'][0]['tracking-pin'][0])) {
echo "> YOU HAVE FAILED... \n";
} else {
echo "> SUCCESS! \n";
}
Expected result:
----------------
`$response['shipment-info'][0]['tracking-pin'][0] = '123456789012'
Actual result:
--------------
$response['shipment-info'][0]['tracking-pin'][0] = ''
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74201&edit=1