- json_decode: documentation ( source)
- print_r: documentation ( source)
- json_encode: documentation ( source)
<?php
$xml_1 = '<namesilo>
<request>
<operation>getDomainInfo</operation>
<ip>55.555.55.55</ip>
</request>
<reply>
<code>300</code>
<detail>success</detail>
<created>2009-01-11</created>
<expires>2017-01-11</expires>
<status>Active</status>
<locked>Yes</locked>
<private>Yes</private>
<auto_renew>Yes</auto_renew>
<traffic_type>Forwarded</traffic_type>
<email_verification_required>No</email_verification_required>
<portfolio>Main Portfolio</portfolio>
<forward_url>https://www.namesilo.net</forward_url>
<forward_type>Temporary Forward (302)</forward_type>
<nameservers>
<nameserver position="1">NS1.NAMESILO.COM</nameserver>
<nameserver position="2">NS2.NAMESILO.COM</nameserver>
</nameservers>
<contact_ids>
<registrant>444444</registrant>
<administrative>555555</administrative>
<technical>666666</technical>
<billing>7777777</billing>
</contact_ids>
</reply>
</namesilo>';
$xml_2 = '<namesilo><request><operation>checkRegisterAvailability</operation><ip>1.1.1.1</ip></request><reply><code>300</code><detail>success</detail><available><domain price="11.79" premium="0">nononononodneodneond.net</domain></available></reply></namesilo>';
$simple_test_1 = new simplexmlelement($xml_1);
$simple_test_2 = new simplexmlelement($xml_2);
function formatResponse($data, $assoc = false)
{
$data = json_decode(json_encode($data));
$data = formatAttributes($data);
return json_decode(json_encode($data), $assoc);
}
function formatAttributes($attributes)
{
foreach ($attributes as $key => $attribute) {
if (is_array($attribute)) {
$attributes->{$key} = formatAttributes((object)$attribute);
$attributes->{$key} = (array)$attributes->{$key};
}
if (is_object($attribute)) {
$attributes->{$key} = formatAttributes($attribute);
if (isset($attribute->{'@attributes'})) {
unset($attributes->{$key}->{'@attributes'});
}
}
if (!is_scalar($attributes->{$key})) {
foreach ($attributes->{$key} as $a_key => $items) {
if (!is_scalar($items)) {
if (count((array) $items) == 1 && is_array($attributes->{$key})) {
$attributes->{$key}[$a_key] = $items->{0};
}
if (count((array) $items) == 1 && is_object($attributes->{$key})) {
if (isset($items->{0})) {
$attributes->{$key}->{$a_key} = $items->{0};
}
}
}
}
}
}
return $attributes;
}
//print_r(formatResponse($simple_test_1));
print_r(formatResponse($simple_test_2));