Re: Urgent help needed.
| From: | Joe Conway | Date: | Sun, 19 Nov 2000 03:29:05 +0000 |
| Subject: | Re: Urgent help needed. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-26098@lists.php.net to get a copy of this message | ||
>*Short Question*
>I need to be able to send a POST using a method other than forms or
sessions....
>Can this be done with headers?
<snip>
>Can I POST without a form if not, what should I do???
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, e.g.
$request = "var1=foo&var2=bar". $rsf is an array of values from a PostgreSQL
query which includes the URL of the target page ($rsf["target_url"]) and the
desired method ($rsf["target_method"]).
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();
};
};