I
I
Ivan2018-12-25 11:33:36
PHP
Ivan, 2018-12-25 11:33:36

How to encode XML to JSON?

I'm trying to recode xml to json and nothing happens, online services recode my xml without problems, like this one .
I am getting bool(false) or string(4) "null" error
My code is:

<?php
header('Content-Type: application/json; charset=utf-8');

//подгружаю xml файл
$xml = simplexml_load_file('https://accounts.clickbank.com/api2/marketplace');
//перевожу его из xml в json
$json = json_encode($xml);
//формирую массив из json
$data = json_decode($json,TRUE);
//$data = json_encode($jsonString, TRUE);
//проверяю как сформировался массив
var_dump($data);

?>

With $data = json_decode($json,TRUE); bool(false) error
when $data = json_encode($jsonString, TRUE); error string(4) "null"
Please help me understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2018-12-25
@9StarRu

In general, in the service that you cited as an example, they use js x2js lib , as I understand it, you need to implement it in php. In theory, it should work the way you did, but try looking at this xml2jsonphp . There article and at the end of a link on .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question