Lab 14: JSON, XML & API Clients

Objective

Master PHP's data interchange formats: json_encode/json_decode with all flags, JSON Schema validation, SimpleXML and DOMDocument for XML, XMLReader for streaming large XML, HTTP API clients using curl with retry logic, and webhook signature verification.

Background

Modern PHP applications consume and produce data in JSON and XML constantly. JSON is the API standard; XML persists in legacy systems, RSS feeds, SOAP services, and government data. PHP's json_encode has 15+ flags that control output. XML has two parsing paradigms: DOM (load entire document into memory) and SAX/streaming (XMLReader) for large files. Understanding both makes you able to handle any integration.

Time

30 minutes

Prerequisites

  • PHP Foundations Lab 12 (JSON & APIs)

Tools

  • Docker: zchencow/innozverse-php:latest


Lab Instructions

Step 1: JSON advanced — flags, schema validation, streaming


Step 2: XML parsing + HTTP client with retry

📸 Verified Output:


Summary

Tool
API
Use for

json_encode

JSON_PRETTY_PRINT, JSON_THROW_ON_ERROR

Encode with control

json_decode

associative: true, JSON_THROW_ON_ERROR

Decode safely

simplexml_load_string

->xpath()

Read/query XML

DOMDocument

createElement, setAttribute

Modify XML

XMLReader

Streaming

Large XML files

hash_hmac

hash_equals

Webhook verification

Further Reading

Last updated