Php

How to Consume XML Rest API Using PHP

This lumen tutorial help to create a rest wrapper using lumen to consume XML type Rest API. I have already shared a tutorial to consume JSON type rest API but this example will use XML type rest service that sends XML as a response.

XML is another popular data format to read input and send response output using web-service. The XML format can be used for rest or SOAP type web service.

I am using the lumen framework to consume XML type restful web service. If you are not familiar with lumen or laravel, please read the below tutorials:

You can also use the same mechanism for CURL or other rest PHP frameworks.

Related Post

You can also check other recommended tutorials of Lumen/Laravel,

We will create a client wrapper for the rest server. We will use webservice host url and set header information.

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'restapi url', //https://hostname/api/
    // You can set any number of default request options.
    'timeout'  => 2.0,
 'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json"],
    //ssl false
    'verify' => false
]);

We will pass XML rest endpoints to the client get method and get rest response body content,

$response = $client->get("computer/api/xml")->getBody();

Now we will parse XML data and convert it into JSON data using json_encode. For data processing, I have converted JSON into PHP array using json_decode method.

$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories