How to use SimpleXML in PHP

SimpleXML Objects are pretty easy to handle XML information in PHP. You can use Simple XML if you want a simple way to treat XML data under PHP.

The basis

For example, imagine we are using a Freshbooks API. It uses requests and responses in XML format, so we can easily consume that responses with SimpleXML objects.

For example, this is the response after running client.list function.

<?xml version="1.0" encoding="utf-8"?>
<response status="ok">
  <clients page="1" per_page="15" pages="3" total="42">
    <client>
      <client_id>13</client_id>
      <organization>ABC Corp</organization>
      <username>janedoe</username>
      <first_name>Jane</first_name>
      <last_name>Doe</last_name>
      <email>janedoe@domain.com</email>
    </client>
  </clients>
</response>

So, how to consume this response using XML?

Advertisement

Did you like it?

No comments yet.

Leave a Comment