Search code examples
phparraysapisimplexmlnamecheap

Pull values from SimpleXMLElement Object


I'm using the namecheap API to do some stuff, it's the first time I've used a API and I'm running into a bit of a problem.

This is what I have so far:

$ApiKey = "**********************";
$ApiUser = "*****";
$UserName = "*********";
$ClientIP = "********";
$NamecheapURI = "https://api.namecheap.com/xml.response";

$executionURL = $NamecheapURI."?ApiUser=".$ApiUser."&ApiKey=".$ApiKey."&UserName=".$UserName."&Command=namecheap.domains.check&ClientIp=".$ClientIP."&DomainList=".$domain;

$xml = simplexml_load_file($executionURL);

print_r($xml);

When print $xml I am returned simple XML objects:

  SimpleXMLElement Object
  (
  [@attributes] => Array
    (
        [Status] => OK
    )

  [Errors] => SimpleXMLElement Object
    (
    )

  [Warnings] => SimpleXMLElement Object
    (
    )

  [RequestedCommand] => namecheap.domains.check
  [CommandResponse] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [Type] => namecheap.domains.check
            )

        [DomainCheckResult] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [Domain] => facebook.com
                        [Available] => false
                        [ErrorNo] => 0
                        [Description] => 
                        [IsPremiumName] => false
                        [PremiumRegistrationPrice] => 0
                        [PremiumRenewalPrice] => 0
                        [PremiumRestorePrice] => 0
                        [PremiumTransferPrice] => 0
                        [IcannFee] => 0
                        [EapFee] => 0
                    )

            )

    )

  [Server] => PHX01APIEXT03
  [GMTTimeDifference] => --5:00
  [ExecutionTime] => 0.008
)

My question is beyond this, how do I move forward and pull data from this?

I've tried treating this as an array but I am getting nowhere, when using is_array() to test if it was an array it says it's not which I don't understand...

I apologise if this is a noob question, I am a bit new to this. In short, what do I need to do to pull data from this?

Thanks in advance!


Solution

  • Learning to use SimpleXML is much better than trying to convert it to arrays/json/anything else and simple (hence the name). A quick example...

    $response = '<?xml version="1.0" encoding="UTF-8"?>
    <CommandResponse Type="namecheap.domains.check">
        <DomainCheckResult Domain="facebook.com">
            <Element>1234</Element>
            <Element>12345</Element>
        </DomainCheckResult>
    </CommandResponse>';
    $xml = simplexml_load_string($response);
    
    echo "DOmain=".$xml->DomainCheckResult['Domain'].PHP_EOL;
    foreach ( $xml->DomainCheckResult->Element as $value)   {
        echo "Value=".(string)$value.PHP_EOL;
    }
    

    outputs...

    DOmain=facebook.com
    Value=1234
    Value=12345
    

    You have to adapt this to your own XML, but the idea is that if you want to access an element of an item you use object notation -> and if you need to get an attribute, use array notation [].

    So in the above code, the first echo ($xml->DomainCheckResult['Domain']) gets the <DomainCheckResult> element and outputs the Domain attribute.

    Then the foreach loop says fetch each <Element> within <DomainCheckResult> and output the value.