[FAQ] Re: POST Method
- From: "Chung Leong <chernyshevsky@xxxxxxxxxxx>" <chernyshevsky@xxxxxxxxxxx>
- Date: 30 Apr 2005 11:17:48 -0700
FF wrote:
> Hello
>
> I would like send variable to php script from URL
> using POST method. Is possible do it ?
> ( <a href="xxx.php">)
>
>
> thanks for your help
>
> FF
I have been meaning to write this one...
---------------------------------------
Q. How do I post a form to another site?
A. Use stream_context_create() to create a HTTP POST context, then open
a connection to the site in question with fopen(), passing the context
as the fourth parameter. Use fread() to read the result from the form
submission.
Example:
$post_vars = array(
'post_var1' => "hello",
'post_var2' => "world"
);
// build the request body
foreach($post_vars as $name => $val) {
$pairs[] = $name . '=' . rawurlencode($val);
}
$body = implode("&", $pairs);
// HTTP options
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-length: " . strlen($body) . "\r\n" .
"Cookie: foo=bar\r\n",
'content'=>$body
)
);
$context = stream_context_create($opts);
$f = fopen('http://localhost/test.php', 'r', false, $context);
In PHP 5, a context can also be passed to file_get_contents() and
file().
Although stream_context_create() is available since PHP 4.3.0, support
for HTTP POST has only become available in 4.3.5. If you are using an
older version, you would need the cURL functions or use fsockopen() to
open the connection and send the request with fputs().
[cURL example here]
.
- References:
- POST Method
- From: FF
- POST Method
- Prev by Date: Re: mass newsletter
- Next by Date: Re: How long is too long for a cgi script?
- Previous by thread: Re: POST Method
- Next by thread: PHPSESSID expires before my cookie
- Index(es):
Relevant Pages
|