parser error : Start tag expected, '<' not found
When simplexml_load_string() throws a "Start tag expected, '<' not found" error, it usually means you are trying to parse content that isn't valid XML, or the content is missing entirely. In the specific scenario of integrating with eBay's API sandbox, this often stems from a mismatch between what the application expects and what the server actually returns.
The error message indicates that the parser encountered something other than an opening tag < at the very beginning of the string passed to it. Looking at your stack trace, line 69 attempts to load $respXmlStr into a SimpleXML object. However, the subsequent warnings reveal the root cause: the response string contains "HTTP/1.1 200 OK".
This is not XML; it is plain text HTTP status information. The simplexml_load_string() function expects an XML document structure (e.g., <Ack>Success</Ack>), not a raw HTTP header line. When the parser hits the first character of your string, it finds 'H' instead of '<', triggering the immediate failure.
Debugging the Response Content
The primary step in resolving this is to inspect exactly what $respXmlStr contains before attempting to parse it. Even if you are using a sandbox environment where responses are typically XML-wrapped, there are edge cases where the API returns raw text or redirects that don't conform to the expected schema.
You should add a debugging block immediately after fetching the response and before calling simplexml_load_string(). If you are using cURL or file_get_contents, ensure you are capturing the body correctly:
// Debugging step: Inspect raw content
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "Raw Response Length: " . strlen($respXmlStr) . "\n";
echo "Raw Response Content:\n" . $respXmlStr;
try {
$respXmlObj = simplexml_load_string($respXmlStr);
} catch (Exception $e) {
// Handle parsing exceptions gracefully if needed
echo "Parsing failed with exception: " . $e->getMessage() . "\n";
}
If the output shows HTTP/1.1 200 OK, your request likely returned an error status code that wasn't handled, or the API documentation requires a specific header to be set before the body contains XML. In many RESTful APIs, a 200 status code with a text body might indicate a redirect or a failure message rather than the data payload you expect.
Handling SimpleXML Return Values
A common pitfall in PHP when using simplexml_load_string() is assuming it always returns an object. The function actually returns false if parsing fails. Your stack trace confirms this with the notices: "Trying to get property of non-object".
When $respXmlObj is false, accessing properties like $respXmlObj->Ack or $respXmlObj->SiteHostedPictureDetails results in these notices because you are trying to access a property on a boolean value. While PHP allows this syntax without crashing, it returns null or empty strings depending on the property access method, leading to cascading errors later in your script (lines 92â94).
You must always validate that the object was successfully created before accessing its children:
$respXmlObj = simplexml_load_string($respXmlStr);
if ($respXmlObj === false) {
// Parsing failed. Log the raw string for inspection.
error_log("XML parsing failed. Raw response: " . $respXmlStr);
exit;
}
// Safe to access properties now
$ack = $respXmlObj->Ack;
$picNameOut = $respXmlObj->SiteHostedPictureDetails->PictureName;
$picURL = $respXmlObj->SiteHostedPictureDetails->FullURL;
Why the Sandbox Might Return Raw Text
The eBay API sandbox is generally robust, but integration issues often arise from incorrect request headers or missing authentication tokens. If the server determines that the request lacks valid credentials or fails a pre-check, it might return a plain text error message instead of wrapping it in an XML envelope defined by the SDK.
Ensure that your Content-Type header is set to application/xml if you are making raw HTTP requests, and verify that any required headers (like X-EBAY-CERT-ID or OAuth tokens) are included in the request. If the API returns a 200 status but with text content, check the documentation for specific error handling patterns where the response body might describe the error rather than returning a structured object.
If you are building a larger application where API integrations are central, consider abstracting these calls into a service layer that normalizes responses before they reach your business logic. This prevents raw HTTP details from contaminating your data models.