1

I have made a PHP script that need to receive the following XML:

<contact_email>people@example.com</contact_email>
<contact_password>hash_in_sha256</contact_password>

I usually do stuff with JSON, but in this project I need to use XML. So, to parse the JSON objects in PHP I use the following code with the following JSON:

{
    "contact_email":"people@example.com",
    "contact_password":"hash_in_sha256"
}

The script:

$jsonBody = file_get_contents("php://input");
$jsonBody = json_decode($jsonBody);

$contact_email = $jsonBody->contact_email;
$contact_password = $jsonBody->contact_password;

But what should I do to have "people@example.com" and "hash_in_sha256" from the XML as a variable in PHP?

1 Answers1

0

Like Sahil Gulati said, use simplexml_load_string, just make sure you have valid xml, there should only be one top node, eg:

$input = "<contact><contact_email>people@example.com</contact_email><contact_password>hash_in_sha256</contact_password></contact>";

$xml = simplexml_load_string($input);

echo $xml->contact_email;
echo $xml->contact_password;