simplexml_load_string not returning element with letter, hyphen and numbers in the name

451 Views Asked by At

I'm using PHP 5.5.15, Codeigniter 3 and trying to save data retrieved from the Adobe Connect xml api. Everything is working fine except there is one element not showing up in the object returned by simplexml_load_string. This element is named x-998066622 and is from a custom user field in Adobe Connect. Other values with hyphens in the name of the element are being successfully gotten. I have changed the data in the returned xml and object to protect privacy.

public function getuserdata(){
    $query = $this->adobe_model->get_principals();
    echo "Be patient, this one takes a while...<br><br>";
    foreach($query->result() as $row){
        $url = 'https://uc-d.adobeconnect.com/api/xml?action=principal-info&principal-id='.$row->principal_id.'&session='.$this->session->userdata['breezesession'];
        $response = $this->get_response($url);
        $xml = simplexml_load_string($response);
        if(strtolower($xml->status['code']) == 'ok'){
            if(is_object($xml)){
                foreach($xml as $key => $value){
                    $data = array();
                    if($key == 'principal'){
                        echo "Updating for ".$value->name."<br><br>";
                        $data['first_name']     = (string)$value->{'first-name'};
                        $data['last_name']      = (string)$value->{'last-name'};
                        $data['company']        = (string)$value->{'x-company'};
                        $data['cost_center']    = (int)$value->{'cost-center'};
                        $data['dafis']          = (string)$value->{'x-998066622'};
                        $data['department']     = (string)$value->{'x-department'};
                        $data['phone']          = (string)$value->{'x-company-phone'};
                        $this->adobe_model->update_principal((int)$value['principal-id'],$data);
                    }
                }
            }else{
                // TO DO: put some error handling here
            } // if(is_object($xml) )
        }else{
            $this->login();
        } // if(strtolower($xml->status['code']) == 'ok')
    }
    echo "<strong>Done.</strong><br>";
    echo "<a href=\"".site_url()."\">Back To main</a>";
}

The XML

<?xml version="1.0" encoding="UTF-8"?>
<results>
    <status code="ok"/>
    <contact>
        <email>[email protected]</email>
        <first-name>hdfhg</first-name>
        <last-name>dghfhfh</last-name>
    </contact>
    <preferences acl-id="852328317" lang="en" time-zone-id="4"/>
    <principal account-id="841422360" disabled="" has-children="false" is-hidden="false" is-primary="false" principal-id="852328317" tos-status="" type="user">
        <ext-login>[email protected]</ext-login>
        <login>[email protected]</login>
        <name>qewrew ewrqwe</name>
        <email>[email protected]</email>
        <first-name>xcbc</first-name>
        <last-name>xbvxcv</last-name>
        <x-company>zvcvxzcv</x-company>
        <x-company-phone>fgfsgsdfg</x-company-phone>
        <x-company-phone-key>sdfgdg</x-company-phone-key>
        <x-department>ssgffgdfg</x-department>
        <cost-center>sdfgf</cost-center>
        <x-998066622>sdfgsfgg</x-998066622>
    </principal>
</results>

Data dump

object(SimpleXMLElement)#3225 (13) {
    ["@attributes"]=> array(8) {
            ["account-id"]=> string(9) "sadfsdf"
            ["disabled"]=> string(0) ""
            ["has-children"]=> string(5) "false"
            ["is-hidden"]=> string(5) "false"
            ["is-primary"]=> string(5) "false"
            ["principal-id"]=> string(9) "sdfasdf"
            ["tos-status"]=> string(0) ""
            ["type"]=> string(4) "user"
        }
        ["ext-login"]=> string(19) "[email protected]"
        ["login"]=> string(19) "[email protected]"
        ["name"]=> string(12) "rerqwe qewrer"
        ["email"]=> string(19) "[email protected]"
        ["first-name"]=> string(6) "qerqwrqw"
        ["last-name"]=> string(5) "qrewrqw"
        ["x-company"]=> string(3) "qerqwer"
        ["x-company-phone"]=> string(14) "qwerqwerewq"
        ["x-company-phone-key"]=> string(10) "wqerwere"
        ["x-department"]=> string(20) "qwerqwerwer"
        ["cost-center"]=> string(9) "841779761"
        ["account-expiry-login-notif"]=> string(5) "false"
}
1

There are 1 best solutions below

0
On

OK got it figured out. I've been at this one all afternoon. Thanks, Phil for testing it.

Here is what is happening: Adobe Connect will not return this field in the XML if it contains no value. Not all profiles have this field filled out. Turns out the ones I tested did not have it filled out. When I let it run through all of the users then I got some results in the field for a few of them.

smacks self on forehead