Sending and receiving XML

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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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, '');

$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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryKevinBanksAuthor Commented:
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 = '' ;
//  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.

GaryKevinBanksAuthor Commented:
Oops! That shoulda been:

if ($mySession = curl_init($connectURL)) {
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

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():
GaryKevinBanksAuthor Commented:
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!
Brilliant glad to hear it. Let us know how you get on I'll be interested to see how it goes.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.