Results 1 to 7 of 7

Thread: Write XML doc from HTTP.POST

  1. #1
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write XML doc from HTTP.POST

    I need to create a script that will write an XML document from a HTTP.POST.
    Ive read a number of threads learning how to write the XML file. I'm just not sure how I would parse the POST and then to write the file on the server.

    Any help is much appreciated.

    The typical POST will look like this:
    01Array (
    02 [variables] => Array (
    03 [response_id] => 123456
    04 [survey_id] => 100001
    05 [survey_title] => Cool survey with all question types
    06 [total_responses] => 23
    07 [start_date] => 2010-02-15 16:27:00
    08 [end_date] => 2010-02-15 17:33:17
    09 [ip_address] => 98.16.84.122
    10 [country] => Ireland
    11 [custom_tags] => Array (
    12 [0] => Array (
    13 [name] => customer_reference
    14 [value] => RT123456
    15 ) )
    16 [answers_xml] =>
    17 <answers>
    18 <question number="1">
    19 <title>Text Single question type</title>
    20 <answer>Some test single text</answer>
    21 </question>
    22 <question number="2">
    23 <title>Text Paragraph question type</title>
    24 <answer>Some test paragraph text</answer>
    25 </question>
    26 <question number="3">
    27 <title>Email question type</title>
    28 <answer>no-reply@gmail.com</answer>
    29 </question>
    30</answers>
    31 ) )

  2. #2
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So after setting up a cross domain policy I am able to capture the http.post from polldaddy's website. I've directed it to this PHP script:

    <?php
    $postdata = file_get_contents("php://input");
    $fp=fopen("tester.xml","w+");
    fwrite($fp,$postdata."\t");
    ?>

    Can someone send me a good tutorial on how I can parse this string to create a well formed XML record?

    tester.xml file:

    variables%5Bresponse_id%5D=14064546&variables%5Bsurvey_id%5D=1766765&variables%5Bsurvey_title%5D=Test+Survey+-+HTTP.POST&variables%5Btotal_responses%5D=6&variables%5Bstart_date%5D=Today%2C+9%3A24PM&variables%5Bend_date%5D=Today%2C+9%3A24PM&variables%5Bip_address%5D=207. 229.137.238&variables%5Bcountry%5D=United+States&variables%5Banswers_xml%5D=%3Canswers%3E%3Cquestion+number%3D%221%22%3E%3Ctitle%3EWhich+of+these+colors+do+you+ like+the+most%3F%3C%2Ftitle%3E%3Canswer%3EOrange%3C%2Fanswer%3E%3C%2Fquestion%3E%3Cquestion+number%3D%222%22%3E%3Ctitle%3EWhich+of+these+sports+do+enjoy+watchin g+on+TV+the+most%3F%3C%2Ftitle%3E%3Canswer%3EBasketball%3C%2Fanswer%3E%3C%2Fquestion%3E%3Cquestion+number%3D%223%22%3E%3Ctitle%3EWhat+is+your+age+range%3C%2Ftit le%3E%3Canswer%3E30-39%3C%2Fanswer%3E%3C%2Fquestion%3E%3C%2Fanswers%3E&variables%5Burl%5D=http%3A%2F%2Fwww.nealdesign.net%2FwriteTest.php

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    I've always wanted some application for array_walk_recursive. Cool beans. Array_walk_recursive() will start at the parent node and work its way through the generations of nodes. For each node it walks through, it adds the required children. $post_array is the POSTed array that you're using.

    Use SimpleXML to start an XML element and continue adding children, then write it into tester.xml.

    PHP Code:
    <?php

    $xml 
    = new SimpleXMLElement('<root/>');

    array_walk_recursive($post_array, array ($xml'addChild'));

    // Then just write it to your XML sheet.
    $postdata file_get_contents("php://input");

    // Append your XML data to the sheet.
    $postdata .= $xml->asXML();

    $fp=fopen("tester.xml","w+");
    fwrite($fp,$postdata."\t");

    ?>
    - Josh

  4. #4
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the help.

    Is this supported by PHP Version 4.4.9?
    It doesn't seem to be creating the tester.xml file.

  5. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Post your complete code.
    - Josh

  6. #6
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    My apologies. I thought the code you added in the previous post was complete.

    Disclaimer: I have zero experience in PHP and am hacking this together.

  7. #7
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    I'm not sure how else to help you then since I don't know how your POST data is being sent to the script that is executing. $post_array is supposed to contain the array that is to be converted into XML.

    At the top of the script, you need to define $post_array with the value of the POSTed data.

    Example:
    PHP Code:
    <?php

    $post_array 
    $_POST['**post name here**'];
    - Josh

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •