Use form to create Xml variable and return results

Discussion in 'PHP' started by Riots, Aug 16, 2014.

  1. #1
    Ok here is the scenario im trying to create. This will be real basic.
    A user will use a form to search. and return results.

    The form will have to create a xml variable and submit that to a webservice url and it will return results.

    Here are the requirements of the xml variable
    Pass the following request XML to the feed URL in the variable called XML.
    <FDSRequest>
    <username>username</username>
    <password>password</password>
    <sType>GNMAR</sType>
    <detail>1</detail>
    <testmode>true</testmode>
    <searchParams>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
    <state>FL</state>
    </searchParams>
    </FDSRequest>

    Feed URL -myplace.com/feeds/RDSFeed.cfm
    (GET and POST methods accepted)

    So far I just have a form with the required fields to be searched.
    Can someone help me come up with some code or point me in the right direction.
    I would be gladly to pay for your time if you can help me.
     
    Riots, Aug 16, 2014 IP
  2. spujap

    spujap Active Member

    Messages:
    136
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Though this question is posted in PHP section but you can capture form data in Jquery, then send a AJAX request to the service using JQuery and XML processing can be done using Javascript-DOM.

    Here are few links that you can use to implement it using Javascript & Jquery

    Create XML - http://stackoverflow.com/questions/3191179/generate-xml-document-in-memory-with-javascript
    
    Send AJAX request to web service using the XML that you created - http://stackoverflow.com/questions/3099369/jquery-ajax-post-to-web-service
    
    How to manipulate XML in Javascript -
    
    http://www.w3schools.com/dom/default.asp
    http://oreilly.com/pub/h/2127
    Code (markup):
    If you want to do it in PHP itself you should use Curl to send XML request & recieve XML response -
    http://stackoverflow.com/questions/6337684/php-post-to-rest-web-service

    I am assuming that the service is a REST web service since you have not mentioned it explicitly. But the strange part is that it's using XML as a parameter instead of JSON which is standard now days for REST.

    Might it be a SOAP service you can use PHP's inbuilt SOAP client - http://www.codingfriends.com/index.php/2010/04/16/soap-client-calling-net-web-service/

    SOAP service's request is an XML itself which contains actual request + headers in it.

    To create/manipulate XML use simpleXML - W3schools.com/php/php_xml_simplexml.asp
     
    spujap, Aug 16, 2014 IP
  3. Riots

    Riots Active Member

    Messages:
    374
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    83
    #3
    So i can get results back from the api webservice when i Submit an xml
    How can I create a form that will fill in the blanks in the xml? and I will get new results based on the form input.
    Here is my code.
    <?php
    $request_xml = '
    <YourAPI>
    <username>username</username>
    <password>password</password>
    <sType>uyt</sType>
    <detail>0</detail>
    <testMode>true</testMode>
    <debug>true</debug>
    <searchParams>
    <firstName>robert</firstName>
    <lastName>slim</lastName>
    <city></city>
    <state></state>
    <DOB></DOB>
    </searchParams>
    </YourAPI>';
    
    //Initialize handle and set options
    $username = 'username';
    $password = 'password';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.yourwebservice.com/myapi.cfm');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($request_xml));
    curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    //Execute the request and also time the transaction
    $start = array_sum(explode(' ', microtime()));
    $result = curl_exec($ch);
    $stop = array_sum(explode(' ', microtime()));
    $totalTime = $stop - $start;
    //Check for errors
    if ( curl_errno($ch) ) {
        $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
    } else {
        $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
        switch($returnCode){
            case 200:
                break;
            default:
                $result = 'HTTP ERROR -> ' . $returnCode;
                break;
        }
    }
    //Close the handle
    curl_close($ch);
    //Output the results and time
    echo 'Total time for request: ' . $totalTime . "\n";
    echo $result;
    
    ?>
    PHP:
     
    Riots, Aug 20, 2014 IP