Thursday, March 2, 2017

Using XML data encoded to JSON on PHP

I've just created a php file to query some data from an API.
A normal returned data should be like :

{
@attributes: {
    Status: "OK"
},
Errors: { },
Warnings: { },
RequestedCommand: "checking",
CommandResponse: {
    @attributes: {
    Type: "check"
    },
    DomainCheckResult: [
        {
        @attributes: {
            Domain: "example1.com",
            Available: "true",
            ErrorNo: "0",
            Description: "",
            IsPremiumName: "false",
            PremiumRegistrationPrice: "0",
            PremiumRenewalPrice: "0",
            PremiumRestorePrice: "0",
            PremiumTransferPrice: "0",
            IcannFee: "0",
            EapFee: "0"
        }
        },
        {
        @attributes: {
            Domain: "example2.com",
            Available: "true",
            ErrorNo: "0",
            Description: "",
            IsPremiumName: "false",
            PremiumRegistrationPrice: "0",
            PremiumRenewalPrice: "0",
            PremiumRestorePrice: "0",
            PremiumTransferPrice: "0",
            IcannFee: "0",
            EapFee: "0"
        }
        },
    ]
},
Server: "asd",
GMTTimeDifference: "--5:00",
ExecutionTime: "2.622"
}

Now, When I try to get the attributes values such as "Domain" and "Available" and the other ones to put them in some variables, I do the following :

$client = new Client();
        $res = $client->request('POST', 'http://api.website.com/xml.response', [
        'form_params' => [
        'ApiUser' => 'myuser',
        'ApiKey' => 'apikey',
        'UserName' => 'myuser',
        'ClientIp' => 'myip',
        'Command' => 'check',
        'List' => $list
            ]
        ]);
        $xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
        $json = json_encode($xml);
        $data = json_decode($json, true);
        echo $data->CommandResponse[0];

But it tells me :

Trying to get property of non-object

What am I doing wrong? Please help me. Thank you.



via M. Safari

Advertisement