Categories: How ToMagento 2

How to Convert CURL XML Response into an Array in Magento 2

When it comes to secure and reliable to E-commerce platform that provides good customer experience along with lightning fast speed, Magento stands first. Every day a developer wakes up to create & develop something new to improve existing Magento E-commerce experience in such a way that can help store owners to increase revenue as well as enhance the customer shopping experience. By default, Magento 2 is very secure & handy with easy to use design interface that easily interact with various API and gateways to send & receive data. Recently we have been working on Magento based CURL project with one of the gateways in which we have to send data to the server and store received response into an object so we can easily use that response for the further development.

Also, meanwhile developing shipping & gateway extension, we came across the requirement where we need to get all the response data in an array and wanted to display it in such format.
So, here is how we have converted CURL XML response into an array in Magento 2 without any kind of hardcoding.

All you need to do is, just load up the XML in SimpleXML and use it directly as shown below.

 $ch = curl_init();
 $sendurl = "http://example.com";
 curl_setopt($ch, CURLOPT_URL, $sendurl);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $data = curl_exec($ch);
 curl_close($ch);
 $response = preg_replace("/(]*>)/", "$1$2$3", $data);
 $xml = new \SimpleXMLElement($data);
$array = json_decode(json_encode((array)$xml), TRUE);
print_r($array);

Now you know that how to store response, you can try it on different XML feeds from various APIs. Hit the stars below if it worked for you and comment down if you face any issue while implementing the code.
Happy Coding!

Click to rate this post!
[Total: 11 Average: 4.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago