Submit xml request to any url in php

This topic will help php developer to submit request to any url using curl. In this example we will submit the xml data to the given url.

$xmlcontent=' 
api_username
api_password

your accid data ';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, "https://yoururl.com/api-request");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$xmlcontent);
$content=curl_exec($ch);

In the above request we are submitting the $xmlcontent to this “https://yoururl.com/api-request” url. Now $content is the response object i.e is in XML. We can convert it into array for flexibility of user requirements:

$vararray=simplexml_load_string($content);
$vararray=(array)$vararray;

The above code will help you to convert xml data to an associative array. Hope the code will help to the php developer for curl request or convert xml to array in php. Thanks for reading the post.

Leave a Reply

Your email address will not be published. Required fields are marked *