php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74201 Strange SimpleXMLIterator Behaviour
Submitted: 2017-03-03 02:20 UTC Modified: 2017-03-03 09:00 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: dac514 at hotmail dot com Assigned:
Status: Wont fix Package: SimpleXML related
PHP Version: 5.4.35 OS: CentOS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dac514 at hotmail dot com
New email:
PHP Version: OS:

 

 [2017-03-03 02:20 UTC] 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/10000352593/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] = ''

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-03 09:00 UTC] requinix@php.net
-Status: Open +Status: Wont fix -PHP Version: Irrelevant +PHP Version: 5.4.35
 [2017-03-03 09:00 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Oct 27 12:01:22 2020 UTC