php page to xml to remote server

Discussion in 'PHP' started by mathewjenkinson, Jun 10, 2010.

  1. #1
    Hi all,
    Im trying to create an test environment where a php page can complete a XML document and then proceed to post said xml document to a remote server.

    Its based on a telephone database currently stored on a local mysql server. The user clicks the telephone number and this is then inserted into the XML document completing the document. This is then posted to a remote server.

    Ive written the PHP so that when the user clicks from the displayed page, it loads a link to:
    http://localhost/xml_post.php?tel=0123456789 but how do I get the xml_post to insert the string into the pre-written xml and then post it to the server?

    the xml is small and basic:

    <?xml version='1.0' encoding='UTF-8' ?>
    <username>test</username>
    <password>123</password>
    <tel_number>INSERTED STRING</tel_number>


    Basically it goes:

    HTML link-> PHP -> XML -> database

    any help would be appericiated :)
    thank you!
    Mathew
     
    mathewjenkinson, Jun 10, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Hi,

    This is quite simple really. You need to use curl. (an example)

    So this is how I would do it.

    1. create an xml file with code like this.

    
    <?php
        
        $file = 'directory/file.xml';
        
        $fh = fopen($file,'w+');
        
        fwrite($fh,'<?xml version='1.0' encoding='UTF-8' ?>
    <username>test</username>
    <password>123</password>
    <tel_number>'.$_GET['tel'].'</tel_number>');
    
        fclose($fh);
    
    ?>
    
    PHP:
    2. Submit the file
    
    <?php
    
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('xmlfile'=>"@$file"));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL,'http://yourremotewebsite.com/page.php');
        curl_exec($ch);
        curl_close($ch);
    
    ?>
    
    PHP:
    3. delete the file again
    
    <?php
    unlink($file);
    ?>
    
    PHP:
    (All of the code would be in the same file. None of it is tested or deemed to be secure. Improvements have to be made)

    Hope this helps.
     
    stephan2307, Jun 10, 2010 IP
  3. mathewjenkinson

    mathewjenkinson Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    WOW,
    lovely thank you very much for your input
    most appericiated
     
    mathewjenkinson, Jun 10, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    No problem. If you have any more problems simply post the code you are using, the problem you encounter and I am sure me or someone else will be able to help you out.
     
    stephan2307, Jun 10, 2010 IP
  5. Kamboodle

    Kamboodle Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thanks, I was actually looking for something similar!
     
    Kamboodle, Jun 10, 2010 IP
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #6
    If you all like this then feel free to add to my reputation. (star button on the very left under each post)
     
    stephan2307, Jun 10, 2010 IP
  7. mathewjenkinson

    mathewjenkinson Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    lol, forgive me im presently struggling with how to install curl onto windows XP WAMPServer 2.0 - but thats by the by.

    The code you have written, im assuming they are all not individual files? more snipits of code that I can put into existing web pages?
    apart from the code that creates the XML file (your top snipit)

    so its still a case of: HTML Page > user clicks link and it creates a url of : post_xml.php?tel=0123456789

    this is then put into post_xml.php and the tel number populates the variable 'tel'
    then using 2nd snipit of code (still inside post_xml.php) the document can then be posted to the server?

    is that right or am I getting the method wrong?

    thanks
    Mathew
     
    mathewjenkinson, Jun 10, 2010 IP
  8. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #8
    Yes all the snippets go on the same page and you can add more code as you want.

    Correct

    Also correct.

    Sorry no idea how to or even if wampp would support curl
     
    stephan2307, Jun 10, 2010 IP