All
Technology
SEO
Design
Programming
iPhone
Android

PHP Handling SimpleXMLElement Errors

    Share on Facebook Create Topic
by jones
I'm writing a simple API to interface with my system. The first task I want my server script to do is verify the XML structure. I'm not sure if defining a xsd or schema is what I need to do as that seems to just define keys and fieldtypes.

My problem is that I use PHP to read the XML string that is being submitted and using strpos to check for the proper tags. This would work if I only had one type of XML call to my server script. However, I have a few and I define the call in the XML <call>UPDATE</call>. So, if I get an UPDATE call then I know there would be some required tags I would need to look for. So, I have to first pull the <call> tag out which requires me to start parsing the XML prematurely.

libxml_use_internal_errors(true);


This still doesn't suppress the fatal error.

I need a way to send a error to the user or another way to get the <call> value from the XML request without parsing it with SimplXMLElement.
Right now, the API client sends the XML request with the <call> tag via cURL. The headers contain POST, content-type, and content-length. Is there a way maybe I can modify the header to contain the specific call that I can retrieve on my API server script?
by BookWorm617
by Danni_boi
Try/Catch ?

http://php.net/manual/en/language.exceptions.php
by Easymanmoney
Ew. Don't do manual XML parsing/validation. If you're working with DOM documents use this:

http://php.net/manual/en/domdocument.schemavalidate.php

Otherwise, use whatever appropriate library PHP provides. Here's a quick search (Google link provided for convenience, not to be snarky).
by Skeletal
Right, I looked at building a validation using the xsd but it seemed overkill because I'm not sure how that validation works with different request XML. For instance, a user may call UPDATE in the request XML. The XML may look like this:


$headers = apache_request_headers();
$call = $headers['Call'];
  1. $headers = apache_request_headers();
  2. $call = $headers['Call'];



I could then feed the call into my api class which could then load the xsd schema needed to validate. Any problems or pitfalls with this course?
Home | Contact | Privacy Policy |