SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php://input example

    Hello,
    could anyone show me how to use the php://input wrapper? I checked the manual but couldn't find the examples helpful.
    I would like to get the rough content of an HTTP POST.

    thanks in advance for your time

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can treat "php://input " as plain filename and use it everywhere filename makes sense (fopen(), file(), file_get_contents() etc).

    Complete example: simple XML-based client-server application. Client part sends xml requests to the server via XMLHTTPRequest. Server part reads xml from php://input and replies it back to the client.

    PHP Code:

    <?php
    // SERVER

    if(getenv('REQUEST_METHOD') == 'POST') {
        
    $client_data file_get_contents("php://input");
        echo 
    "
            <SERVER>
                Hallo, I am server
                This is what I've got from you
                
    $client_data
            </SERVER>
        "
    ;
        exit();
    }
    ?>

    <!-- CLIENT -->

    <script>
    function sendAndLoad(sURL, sXML) {
        var r = null;
        if(!r) try { r = new ActiveXObject("Msxml2.XMLHTTP") } catch (e){}
        if(!r) try { r = new XMLHttpRequest() } catch (e){}
        if(!r) return null;
        r.open("POST", sURL, false);
        r.send(sXML);
        return r.responseText;
    }
    </script>

    <button onclick="
        alert(sendAndLoad(
                location.href,
                '<xml><sample>data</sample></xml>'
        ))">
    Test
    </button>

  3. #3
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much
    will try that.

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey I have one more question
    Since you seem used to this process of making two apps communicating with each other, I was wondering, how would you typically handle incoming xml documents?

    I mean, if an application send to my server an xml document in the body of a HTTP POST, what is the best way to handle it?

    Here is a typical example:
    Code:
    <Message>
      <text>hello</text>
      <user>benno</user>
      <lang>en</lang>
    </Message>

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use DOM_XML (php4) or DOM (php5) functions

    http://www.php.net/ref.domxml
    http://www.php.net/ref.dom

  6. #6
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow... that's some new horizons

    I'll check it out all. Any hint (trick) to get the value of a given tag maybe?

    you're such a great help man!

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To get any given tag (or node) in a DOM tree, you can use DOM navigation functions (for example, http://www.php.net/function.dom-domd...getelementbyid) or XPath (http://www.php.net/function.dom-domxpath-query).

  8. #8
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great!
    that was some serious and quick help.
    thank you very much for your patience and your assistance.
    Regards.


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
  •