Sending and receiving XML

Posted on 2008-11-13
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
    LVL 1

    Accepted Solution

    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


    Author Comment

    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.


    Author Comment

    Oops! That shoulda been:

    if ($mySession = curl_init($connectURL)) {
    LVL 1

    Assisted Solution

    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

    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!
    LVL 1

    Assisted Solution

    Brilliant glad to hear it. Let us know how you get on I'll be interested to see how it goes.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why spend so long doing email signature updates?

    Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
    This video teaches users how to migrate an existing Wordpress website to a new domain.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now