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?
mattayers08Connect With a Mentor Commented:
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

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)) {
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.