[FAQ] Re: POST Method




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]

.



Relevant Pages

  • Re: Tokenizer Difficulties
    ... > I've delved into the usage of the PHP Tokenizer that directly ... > PHP file. ... Without the context, you simply can't do the job right. ... the "parent context" (your visibility declarations are either in variable ...
    (comp.lang.php)
  • Re: Do people still use the .inc extension?
    ... To be safe, you can add some checks, I personnaly apply the one used in phpBB: ... in the php includes. ... if your file does some code depending on a context, which context doesn't exist if you call that file directly, yes. ... Actually, the execution of the code would be quite predictable, and, if properly coded, should fail anyway if a required variable, etc. is missing. ...
    (comp.lang.php)
  • Re: [PHP] The Context of 0
    ... [PHP] The Context of 0 ... haystack a Boolean FALSE is returned. ... [/small rant] ...
    (php.general)
  • Re: Post with redirect?
    ... and it can't be done with PHP. ... the only thing jerry got right is that it would be a lot of work. ... Because you shouldn't be quoting out of context. ...
    (comp.lang.php)
  • Re: [PHP] Comparison Problems with 5.2.5
    ... On Sun, December 30, 2007 8:34 am, Magnus Anderson wrote: ... Either I have something wrong in my code or PHP 5.2.5 that I user is ... I have recently installed PHP on my windows machine ... View this message in context: ...
    (php.general)