json_decode not working ?


I have the following scripts :

$jsonData = stripslashes(html_entity_decode($xml));





When print_r($jsonData); it produce output as follows :

<whmcsapi version="7.4.1">
<datepaid>0000-00-00 00:00:00</datepaid>
<lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
<description>Domain Renewal - xxxxxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>

But when print_r($k); , no data displayed.

Anybody can help me on this ?

What is wrong with my json_decode ?

Thank you.
Shamsul KamalJunior TechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
The $jsonData you show above is XML and not JSON.  There is no JSON for json_decode to work with.
F IgorDeveloperCommented:
The XML data (xml string) could be converted to a json (string)  structure, and then to an array structure using this code:
(in this case your $jsonData will be $xml_string)

//xml object
$xml = simplexml_load_string($xml_string);
//object to json
$json = json_encode($xml);
//json to array (2nd argument of json_decode() is TRUE, otherwise it will return an object)
$array = json_decode($json,TRUE);

Open in new window

Note: Only tags and text content will be processed, xml attributes will be treated as an array element (@attributes) .

    [@attributes] => Array
            [version] => 7.4.1

    [action] => getinvoice
    [result] => success
    [invoiceid] => 242347
    [invoicenum] => Array

    [userid] => 25209
    [date] => 2017-12-21
    [duedate] => 2018-01-11
    [datepaid] => 0000-00-00 00:00:00
    [lastcaptureattempt] => 0000-00-00 00:00:00
    [subtotal] => 50.00
    [credit] => 0.00
    [tax] => 3.00
    [tax2] => 0.00
    [total] => 53.00
    [balance] => 53.00
    [taxrate] => 6.00
    [taxrate2] => 0.00
    [status] => Unpaid
    [paymentmethod] => banktransfer
    [notes] => Array

    [ccgateway] => Array

    [items] => Array
            [item] => Array
                    [id] => 304494
                    [type] => Domain
                    [relid] => 52025
                    [description] => Domain Renewal - xxxxxx.com - 1 Year/s (11/01/2018 - 10/01/2019)
                    [amount] => 50.00
                    [taxed] => 1


    [transactions] => Array


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shamsul KamalJunior TechAuthor Commented:
Thank you ....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.