7/11/2011

send POST php

To send GET request to another page is easy...

header("location: page2.php?string=hello");


But how do I send/redirect POST request to page2.php??

IMPORTANT: you need to have the cURL library installed for this to work!

page1.php
- - - - - - - - - - - -
<?php
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://fullurl/page2.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, 'firstName=John&lastName=Doe ');
curl_exec ($c);
curl_close ($c);
?>

page2.php
- - - - - - - - - - - -
<?php
$fn = $_POST['firstName'];
$ln = $_POST['lastName'];
// test to see that it really works
file_put_contents('post.txt', $fn . $ln);
?>

Source(s):

All the information you need: http://www.php.net/curl
 
Your POST variables should be preserved if you use header() to redirect to a different page; Location sends the browser a 302 moved permanently response code.

If that doesn't work, you could try sending a 307 moved temporarily http response:

header("HTTP/1.0 307 Temporary redirect");
header("Location:page2.php");

SHARE THIS

Author:

Tôi là PHƯỚC kỹ sư công nghệ thông tin. chuyên thiết kế web và làm dịch vụ MMO.

0 comments: