×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Coding

Sending data to a CGI via POST by sleipnir214
Posted: 24 Sep 02 (Edited 11 Jun 07)

CODE

<?php
/*
    There are two fundamental ways for PHP to send data
    to another CGI via the POST-method:  CURL and fopen.
    
    Where CURL is the easier of the two, fopen is more
    commonly available.
    
    Check the output of phpinfo() to see whether CURL is
    available on your system.


    DISCLAIMER
    This script is intended only to demonstrate a
    possible solution, not to provide a plug-and-play
    solution to a problem.  It will nearly certainly
    not work on your system until you have modified it.
    
    This source code comes without warranty of any kind.
    It might work as-is and solve your problem.
    It might also cause your system to fail
    catastrophically and spectacularly.
    Use with circumspection and trepidation.
    END DISCLAIMER
    
*/



/*
    This method uses CURL to contact the server.
    
*/


//    Either 'http' or 'https'. 'https' is only an
//    option if OpenSSH is available on your system.
//    Check phpinfo() to see whether HTTPS is available.
$HTTP_method = 'http';

//    IP-resolvable FQDN of the server
$hostname = 'hostname.myrurl.com';

//    Path on that server to the CGI
$cgi = '/mypath/mycgi';

//    Array of data.  The foreach loop below is going to
//    construct a field/data string like the one you see
//    in the URL of a GET-method CGI.
$my_data = array (                            
                    'field1' => 'value1',    
                    'field2' => 'value2',
                    'field3' => 'value3',
                    'field4' => 'value4'
                );

//    This section constructs the field/value pairs of
//    the form:
//    field1=value1&field2=value2&field3=value3
$data_string = ';
$add_ampersand = FALSE;
foreach ($my_data as $key => $value)
{
    if ($add_ampersand)
    {
        $data_string .= '&';
    }
    $data_string .= $key . '=' . $value;
    $add_ampersand = TRUE;
}

//    Get a CURL handle
$curl_handle = curl_init ();

//    Tell CURL the URL of the CGI
curl_setopt ($curl_handle, CURLOPT_URL, $HTTP_method . '://' . $hostname . $cgi);

//    This section sets various options.
//    See http://www.php.net/manual/en/function.curl-setopt.php
//    for more details
curl_setopt ($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_POST, 1);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string);
    
//    Perform the POST and get the data returned by the
//    server.
$result = curl_exec ($curl_handle) or die ('There has been an error');

//    Close the CURL handle
curl_close ($curl_handle);

//    Process the return
print $result;






/*
    This method uses fopen to contact the server.
    
    Frankly, I haven't figured out how to do HTTPS
    POST-method inputs yet.
    
*/

//    IP-resolvable FQDN of the server
$hostname = 'hostname.myrurl.com';

//    Path on that server to the CGI
$cgi = '/mypath/mycgi';

//    Array of data.  The foreach loop below is going to
//    construct a field/data string like the one you see
//    in the URL of a GET-method CGI.
$my_data = array (                            
                    'field1' => 'value1',    
                    'field2' => 'value2',
                    'field3' => 'value3',
                    'field4' => 'value4'
                );


//    This is the beginning of the HTTP request header
$send_string =
'POST $cgi HTTP/1.1' . "\r\n" .
'Host: $hostname' . "\r\n" .
'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
'Content-Length: ';

//    This section constructs the field/value pairs of
//    the form:
//    field1=value1&field2=value2&field3=value3
$data_string = "";
$add_ampersand = FALSE;
foreach ($_GET as $key => $value)
{
    if ($add_ampersand)
    {
        $data_string .= '&';
    }
    $data_string .= $key . '=' . $value;
    $add_ampersand = TRUE;
}

//    Complete the send string once we know how long our input string is.
$send_string .= strlen($data_string) ."\r\n\r\n" . $data_string . "\r\n\r\n";

//    Open the socket to the server
$fh = fsockopen ($hostname, 80);

//    Send the data
fputs ($fh, $send_string);

//    Get the return
$return = fread ($fh, 4096);

//    Close the socket
fclose ($fh);

//    Process the return
$return_array = preg_split ('/\r\n/', $return);

?>

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close