Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sending Post data with fsockopen

    hey guys, having major problems with this and its beggining to annoy me now.

    I need to send post data using php to our server and handle the response. At present the code i have is

    PHP Code:
        $postdata 'username=test&key=gdfghsjkhtr234ir';
        
        
    $fp fsockopen("test.servername.co.uk"80$errno$errstr30);
        
    fputs($fp"POST /testapisystem2.php HTTP/1.0\r\n");
        
    fputs($fp"Host: test.servername.co.uk\r\n");
        
    fputs($fp"Content-type: application/x-www-form- urlencoded\r\n");
        
    fputs($fp"Content-length: " strlen($data) . "\r\n");
        
    fputs($fp"User-agent: Mozilla/4.0 (compatible: MSIE 7.0; Windows NT 6.0)\r\n");
        
    fputs($fp"Connection: close\r\n\r\n");
        
    fputs($fp$postdata);
        while (!
    feof($fp)) {
            
    $buf .= fgets($fp,128);
        }
        
    fclose($fp);
        
        echo 
    $buf
    The request is received by the script as shown by the logs for the server however there are no post variables received!

    on the receiving script I have performed a print_r($_POST) and there are no post variables at all!!

    I cant use cURL as not all servers have this enabled and I need to provide the php framework to other people to use.

    so does anyone have any ideas why the post vars are not getting to the file?

    I have also tried the script on a different server (different OS as well as configuration) and still get the same problem
    Last edited by neomaximus2k; 03-04-2009 at 12:04 PM.
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)

  • #2
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I finally resolved this, the data wasn't in the $_POST[] variables but it was in the $GLOBALS['HTTP_RAW_POST_DATA'] variable, then it is a case of exploding the values with a & seperator
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You could also use:
    PHP Code:
    $raw_data file_get_contents'php://input' ); 
    ...which is the same as:
    PHP Code:
    $raw_data $GLOBALS['HTTP_RAW_POST_DATA']; 
    Then, instead of exploding on the ampersands (hacky), use parse_str():
    PHP Code:
    parse_str$raw_data$_POST );

    print_r$_POST ); 
    Last edited by kbluhm; 03-04-2009 at 03:49 PM.

  • #4
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for that, my next problem is doing an sftp call with curl but i'll post a seperate thread for that
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)


  •  

    Tags for this Thread

    Posting Permissions

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