|
Posted by NC on 11/06/41 11:44
lawrence k wrote:
>
> If I've a big string and I want to send it to another web page as a
> GET request, I just use file(). I've done this often.
>
> But what command do I use when I want to send the same string
> as a POST request?
You just send a POST request:
$content = '';
$flag = false;
$post_query = 'a=1&b=2'; // name-value pairs
$post_query = urlencode($post_query) . "\r\n";
$host = 'www.somehost.com';
$path = 'path/to/some/script.php';
$fp = fsockopen($host, '80');
if ($fp) {
fputs($fp, "POST $path HTTP/1.0\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-length: ". strlen($post_query) ."\r\n\r\n");
fputs($fp, $post_query);
while (!feof($fp)) {
$line = fgets($fp, 10240);
if ($flag) {
$content .= $line;
} else {
$headers .= $line;
if (strlen(trim($line)) == 0) {
$flag = true;
}
}
}
fclose($fp);
}
Now $headers contains HTTP headers returned by the remote server, while
$content contains the body of the response (the document you are trying
to retrieve).
Cheers,
NC
Navigation:
[Reply to this message]
|