I am using PHP to make API requests. Why do I see stray characters in my XML response?
The stray characters are indicators that the HTTP response is chunked which the PHP function employed is not able to handle.
So what is chunking?
HTTP 1.1 evolved to address new needs and overcome shortcomings of HTTP 1.0. Generally speaking, it is a superset of HTTP 1.0. One of the improvements over HTTP 1.0 is - Faster response for dynamically-generated pages, by supporting chunked encoding, which allows a response to be sent before its total length is known.
If a server wants to start sending a response before knowing its total length, it might use the simple chunked transfer-encoding, which breaks the complete response into smaller chunks and sends them in series. You can identify such a response because it contains the "Transfer-Encoding: chunked" header. All HTTP 1.1 clients must be able to receive chunked messages.
A chunked message body contains a series of chunks, followed by a line with "0" (zero), followed by optional footers (just like headers), and a blank line. Each chunk consists of two parts:
a line with the size of the chunk data, in hex and ending with CRLF.
In some cases a server or client may want the older HTTP 1.0 behavior. In those circumstances a Connection: close header can be added to inform the receiving party that the persistent connection should not be used again. Alternatively you could use cURL because it supports the newer HTTP/1.1 standard. The other PHP functions do not.