1

After sending a post request to an api I get the following response body:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<header xmlns="mfp:anaf:dgti:spv:respUploadFisier:v1" dateResponse="202211011543" ExecutionStatus="0" index_incarcare="5001131564"/>

I need the index_incarcare value, how can I get it in a variable using php?

tvladan
  • 39
  • 6

2 Answers2

1

You can get to that value using xpath - you just have to mind your namespaces:

$str = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<header xmlns="mfp:anaf:dgti:spv:respUploadFisier:v1" dateResponse="202211011543" 
        ExecutionStatus="0" index_incarcare="5001131564"/>
XML;
$data = simplexml_load_string( $str, 'SimpleXMLElement', LIBXML_NOCDATA);
$data->registerXPathNamespace('xxx', "mfp:anaf:dgti:spv:respUploadFisier:v1");

$inca = $data->xpath('//xxx:header/@index_incarcare')[0];
echo $inca;

Output, based on the xml in your question should be

5001131564
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
0

while jumping here for some help, I continued to search for a solution. The solution I found is:

$response = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<header xmlns="mfp:anaf:dgti:spv:respUploadFisier:v1" dateResponse="202211011543" ExecutionStatus="0" index_incarcare="5001131564"/>
'; //from cURL Post request 
$xml=simplexml_load_string($response) or die("Error: Cannot create object");
function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
}
print xml_attribute($xml, 'index_incarcare')
// returns 5001131564

the solution proposed above is just as good.

tvladan
  • 39
  • 6