Forum Moderators: coopster
Now, it looks like this is a problem that would be solved with sockets. I am confused about how to create the header information though.
Is anyone aware of a good source of info, or a tutorial that would help me to get my head around this? I have not had much luck searching...
Thanks,
WBF
function post($host, $path, $data) {
$http_response = '';
$content_length = strlen($data);
$fp = fsockopen($host, 80);
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: $content_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
while (!feof($fp)) $http_response .= fgets($fp, 28);
fclose($fp);
return $http_response;
}$postdata = '?foo=bar';
foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val;$http_response = post('example.org', '/path/to/script.php', $postdata);
?>
$http_response will return true or false.
Have fun!
[edited by: coopster at 10:14 pm (utc) on Mar. 4, 2005]
[edit reason] fixed typo [/edit]
function post($host, $path, $data) {
$http_response = '';//initialize variable
$content_length = strlen($data); //declare size of POST variable strings
$fp = fsockopen($host, 80); //connect to host domain, port 80
//begin http headers?
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-rlencoded\r\n");
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");//what are we closing here?
//end http headers?
fputs($fp, $data); //transfering the POST data
while (!feof($fp)) $http_response .= fgets($fp, 28);//what are we doing on this line?
fclose($fp); //closing the socket connection
return $http_response; //How/why does this get set true false in the while loop?
}
$postdata = '?foo=bar'; //necessary to initialize this variable? Or just use $_POST..
foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val; //..as here?
$http_response = post('example.org', '/path/to/script.php', $postdata); //I recognize a function call :)
?>
I want to understand what I am doing and why. Really do appreciate the help here.
WBF
You figured most of it out :)
while (!feof($fp)) $http_response .= fgets($fp, 28);//This gets the output of the script, but only 28 bytes. You can test it to see if it was successful. if(!$http_response) print "Failed!";
return $http_response; //How/why does this get set true false in the while loop?
}
$postdata = '?foo=bar'; //I do this just to start the query string with the? and then we can simply use & in the for loop. The var is useless but it doesn't hurt anything.
foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val; //now here we just append all vars with the &
You may want to double check the $http_response part. I can't remember if it's true/false or not. If it's just string data, test it with isset() and empty()
I'm definately not an expert on this so toy around with it and see what it does.
Regards,
Birdman