Sending and receiving XML

Posted on 2008-11-13
Medium Priority
Last Modified: 2013-11-19
I'm a newbie with XML but have to incorporate send and recive chunks of infomation to and from a 3rd party, secure, server.

Have, sort of (!!), got to grips with XMLHTTPRequest object and have tested scripts and connections to and from test pages on my own server but now need to connect to and share data with a 3rd party in order to populate pages on my own site with dynamic data from the 3rd party.

Initially I need to authenticate my connection to the 3rd party befoe I can think of sending requests for further data sets so I have to send an initial handshake in the form of an element, with 1 attribute and one child element with 2 attributes all contained within one root <message> element.

This may be XML101 to some people but I'm really struggling to see the wood for the trees. Any assistance to get me going or reference to tutorials that I can take a look at would be REALLY welcomed.

Many thanks.

Question by:GaryKevinBanks
  • 3
  • 3

Accepted Solution

mattayers08 earned 2000 total points
ID: 22949563
If you're sending and receiving data I'd suggest looking up CURL.
This can then be controlled all via PHP, instead of AJAX.

CURL example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');

$lResponse = curl_exec($ch);

To read XML in PHP use DomDocument(), for example:

$oDom = new DomDocument();
$oDom->loadXML( $sStringOfXML );

You can then do such things as:
$oDom->getElementByTagName( $sTagNameHere ); Which will then give you an array of all XML nodes with that tag name.

Hopefully that'll point you in the right direction.

Matt Ayers


Author Comment

ID: 22949743
Thanks Matt, all useful info. Have just scanned a few pages detailing curl but still in a bit of a quandry!

I can initiate the contact with the 3rd party server:

$connectURL = 'https://3rdpartyserver.name' ;
//  and then block out the http headers to, hopefully, just get xml
if ($mySession = curl_init($url)) {
curl_setopt($mySession, CURLOPT_HEADER, false);
curl_setopt($mySession, CURLOPT_RETURNTRANSFER, true);

but I'm not sure how to fire off my user details in order to authenticate a connection as required by the 3rd party. They require that I log on to authenticate a session in order to send me a sessionID that has to be used when sending over further requests during that session.

They are expecting an XML "handshake" like:
<callerRequest requestCode="authenticate">
<caller callerName="USERNAME" callerPassword="PASSWORD"/>

I can figure out how to send each element, but not how to include the attributes of each element (i.e. the 2 attributes of caller)! I think I must just be being dumb!!

Does that make sense?

Any thoughts or hints as to where I might look for an answer.

Thanks again for the input so far.


Author Comment

ID: 22949756
Oops! That shoulda been:

if ($mySession = curl_init($connectURL)) {
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

mattayers08 earned 2000 total points
ID: 22951682
You'd probably want to post it like so:

curl_setopt(CURLOPT_POSTFIELDS, array('fieldName' => 'your data here'));

That's sending the full set of data to that page with a give field name, or simply post the raw data:

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

curl_setopt(CURLOPT_POSTFIELDS, $sXMLData);

I think that's what you're looking for.

PHP Reference to curl_setopt():

Author Comment

ID: 22951800
Many thanks again Matt, just tried that and it gets the connection - just get a message about the xml not being well formed (!!). But at least that's a response. Proves that something is working if not what I expected.

Now I can thrash out how they think the simplest of XML is not well formed - obviously something they have hidden and don't share with their partners!

Assisted Solution

mattayers08 earned 2000 total points
ID: 22952024
Brilliant glad to hear it. Let us know how you get on I'll be interested to see how it goes.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question