|
Posted by Chung Leong on 08/16/06 15:07
Charlie King wrote:
> I'm trying to send data to a remote script (a credit card processing
> third party) from my site using POST.
>From the defunct clp FAQ:
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().
Navigation:
[Reply to this message]
|