Jump to content
Larry Ullman's Book Forums

Recommended Posts

Larry & all,

 

I hope you can help point me in the right direction. Having searched far & wide for a solution, I'm beginning to question my sanity!

 

My problem is working out how to handle an XML response from a 3rd party service.

 

Scenario is: I upload a file to the 3rd party service, and receive a page back from the service confirming the upload. This page displays an XML root and some nodes(file location, unique reference, etc). I would like to use php to access the XML, the idea being to update my database record with some of the XML data to show that the file has been uploaded successfully.

 

Question: How can I access the XML using php? All of the examples I have seen elsewhere assume that there is an xml file (eg. response.xml) which can be parsed(using simpleXML for example). However, the output from the server is not an XML file. As far as I can see, it is just an html page displaying XML data(and the view source has been disabled by the 3rd party service).

 

An alternative, along the same lines: If I can't access the displayed XML, would it be possible to use the receipt of the XML response to trigger a database update using php. How would I do that?

 

I would welcome any ideas.

 

thx.

Link to comment
Share on other sites

russellt, is it safe to assume that the response you are receiving from the third party is stored in a PHP variable? I mean, you have to be able to somehow access the data, or there is no way of being able to parse and interpret it.

 

Assuming you can access it, then you should also be able to display it as well. You might have to run it through a function like htmlentities (http://php.net/manual/en/function.htmlentities.php), but a simple echo should work.

 

Anyway, there are two options. Probably the quicker, more lightweight method is to treat the returned XML as a string, and use regexes to parse it. The other would be to treat the XML like XML, and use PHP's DOM methods to access various nodes. To do so, you need to use a PHP library, such as DOMDocument (http://php.net/manual/en/class.domdocument.php).

 

Hope that helps.

  • Upvote 1
Link to comment
Share on other sites

HartleySan,

Thank you for your reply and your ideas. Your first sentence was the real issue. I didn't know how to pull the XML into a php variable. However, I have found a work around which allows me to use the XML response as a trigger to update my database. This means (I think) that the database will only update once the upload has been successful.

 

FYI, I discovered that the server response is a standard html 201 response. This is new to me as I have only just begun to play with SDKs and 3rd party services. I was able to change the server call function such that, rather than deliver a 201 response, the server re-directs to another URL and appends the XML node values to the re-direct URL. Using php, I am now able to assign the appended values to variables using $_GET.

 

(I discovered aftwerwards that Larry touches upon this on page 260 - no doubt he is nodding knowingly as he reads this :P )

 

So, I think I have my answer. Thanks again for your interest.

Link to comment
Share on other sites

HartleySan,

I just don't understand what you mean by 'grab the response'.

 

The 201 response for the uploaded file looks like:

 

<?xml version="1.0" encoding="UTF-8" ?>

-<PostResponse>

<Location>http://path1.serverdomain.com/path2/filename.ext</Location>

<Folder>path1</Folder>

<ID>path2/filename.ext</ID>

<Etag>"random_alphanumeric"</Etag>

</PostResponse>

 

So, how do I grab that and place it inside a variable? I'd much prefer to do that than re-directing the server response and using $_GET. I'm particularly interested in assigning the <Location> node to a variable.

 

 

thx.

Link to comment
Share on other sites

I suppose my follow-up question is how are you seeing the server response, but not able to store it in a variable?

 

If you could please explain exactly how you're viewing/getting this server response, I might be able to help you out better.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...