Hello,
Please tell me How can I send variables from a PHP script to another URL using POST without using forms and hidden variables?
Thanks
Laxy
www.funspot.com
Printable View
Hello,
Please tell me How can I send variables from a PHP script to another URL using POST without using forms and hidden variables?
Thanks
Laxy
www.funspot.com
I believe the point of POST is to be hidden. Maybe you are looking for GET ?
If you don't know what that is look it up in the documentation.
Thanks MoonWolf for your quick reply i will check and get back to you............
You would have to open a direct connection with the server you would want to connect to, and manually send the HTTP POST request containing the variables.
Here's a small function which should be able to handle this.
The above function accepts 3 variables. The $host variable should contain the hostname of the server, ie "www.example.com" or "example.com" without the protocol or leading slash. The $path variable should contain the path to the script you want the data to be posted to. For instance, "/foo/bar/script.php" or "/cgi/process_this.cgi". Finally, the $post_data variable should contain a string which specifies the data you want to post. This is easier to explain by showing an example.Code:function post_page($host, $path, $post_data)
{
// Opens a connection to the server
$fp = fsockopen($host, 80, &$error_number, &$error_string, 120);
// Trigger an error if we failed to connect, else continue
if (!$fp)
{
trigger_error('Connection error: ' . $error_string . ' (' . $error_number . ')', E_USER_ERROR);
return FALSE;
}
// See how many bytes our POST data is
$data_length = strlen($post_data);
// Send our HTTP POST request to the server
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: $data_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $post_data);
// As long as the server returns data, keep getting it in 1k chunks
$output = '';
while(!feof($fp))
{
$output .= fgets($fp, 1024);
}
// Close the connection
fclose($fp);
// Return any data returned by the server
return $output;
}
For instance, if you would use the following function with the following $post_data value:
The script you'd contact would have access to 3 POST variables called "variable_name", "variable_name2", and "variable_name3", with the values "value", "578", and "me" respectively.Code:variable_name=value&variable_name2=578&variable_name3=me
I have not tested the function, but it should work. If you have any problems, reply or PM me.