Re: POST from within a script?
| From: | Joe Conway | Date: | Tue, 15 Aug 2000 03:11:56 +0000 |
| Subject: | Re: POST from within a script? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-11726@lists.php.net to get a copy of this message | ||
>I am trying to write a script that will go through an array of orders and
>start a separate HTTP connection (to execute a CGI on another server on the
>internet) for each one.
>
Here's a quick and dirty function to conditionally GET or POST depending on desired method.
$request is a string of the variables being passed, i.e. var1=foo&var2=bar. $rsf is an array of
values from a PostgreSQL query which includes the URL of the target page.
Hope this helps --
Joe
<?
function process_submission($request,$rsf) {
$request = substr($request,0,strlen($request)-1);
print("Request: " . $request . "<br>\n");
// Build the header
$tgt = substr($rsf["target_url"],strpos($rsf["target_url"],"/" ,7));
print("Target: " . $tgt . "<br>\n");
flush();
if (strtoupper($rsf["target_method"])=="POST") {
$header = "POST " . $tgt . " HTTP/1.0\r\n";
$header .= "Content-type: application/x-www-form-urlencoded\r\n";
$header .= "Content-length: " . strlen($request) . "\r\n\r\n";
} else {
$header = "GET " . $tgt . "?" . $request . " HTTP/1.0\r\n\r\n";
};
print("Header: " . $header . "<br>\n");
flush();
// Open the connection
$host = substr($rsf["target_url"],7,strpos($rsf["target_url"],"/" ,7)
- 7);
print("Host: " . $host . "<br><hr>\n");
flush();
$fp = fsockopen($host, 80, &$err_num, &$err_msg, 30);
if ($fp) {
// Send everything
if (strtoupper($rsf["target_method"])=="POST") {
fputs($fp, $header . $request);
} else {
fputs($fp, $header);
};
// Get the response
print("Response:<br><hr>\n");
flush();
while (!feof($fp)) {
$response = fgets($fp, 128);
print($response);
flush();
};
print("<br><hr>\n");
flush();
};
};