How to Convert XML to JSON in PHP

Cloudmersive
1 min readFeb 9, 2023

--

Looking for an easy way to add an XML to JSON conversion feature to your PHP application? You’re in luck: you can use the free API provided below in this article. All you need to do is copy & paste from complementary, ready-to-run PHP code snippets to structure your API call and use a free-tier API key (get one by registering a free account here) to access the data conversion service.

To get started, first run the following command to install the PHP SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next & last on your list, include the below code to capture your API key and call the function:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDataApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->convertDataXmlToJson($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataXmlToJson: ', $e->getMessage(), PHP_EOL;
}
?>

And that’s all there is to it!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet