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
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form submission from PHP

    Hi

    I need to perform a HTTP POST from a PHP script. Essentially my PHP script is receiving form data and making sure it is complete and valid before submitting it to another site.

    I have used CURL and the streams layer to sucessfully make the POST from PHP, but they act as clients, whereas what I want to do is emulate a true browser form submission by issuing the correct HTTP headers. Here is what I have so far:

    Code:
    <?php
    
    $post_data = 'var1=123&var2=456';
    $content_length = strlen($post_data);
    
    header('POST /test/test.php HTTP/1.1');
    header('Host: localhost');
    header('Connection: close');
    header('Content-type: application/x-www-form-urlencoded');
    header('Content-length: ' . $content_length);
    header('');
    header($post_data);
    
    exit();
    
    ?>
    At the moment, this causes Apache to throw an internal error. I have tested the same code on IIS with the same result. I have also tested the script with PHP 4 & 5 - again same result.

    I have even simplified the script to perform a GET:

    Code:
    <?php
    
    header('GET /test/test.php HTTP/1.1');
    header('Host: localhost');
    
    exit();
    
    ?>
    This too causes a server error.

    What in the blazes am I doing wrong?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Not sure if this would fit your requirements, but you could redirect to a PHP page that immediately submits a form via <body onload()="submitform();"> and let the natural order of things take care of it.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    Just outputting headers in a php script is non-specific and not targeted at anything (actually they are sent to what ever made the http request to your web server to serve up your script file.)

    Curl and stream/socket requests are targeted at a specific URL/port. If you want your curl/socket submission to have a complete set of headers, you must include those headers in the curl/socket output.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply CFMaBiSmAd

    Quote Originally Posted by CFMaBiSmAd View Post
    ...actually they are sent to what ever made the http request to your web server to serve up your script file.
    This is actually exactly what I want to do.

    The idea is much the same in priniciple as when you process a form submission and issue a location header to send the browser to a different page:

    Code:
    <?php
    
    header('Location: http://www.mysite.com/test.php');
    
    ?>
    The only difference is that I want is to issue the headers to make the browser perform a POST request instead, taking the browser to the destination page.

    As I said - CURL and the streams layer act as clients - they make a connection from PHP and they also return the results within PHP which does not affect the browser at all.


  •  

    Posting Permissions

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