Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I post $_POST values

I was wondering if anyone can help with posting $_POST values again. Let's say I post form values to post.php where I can access data by $_POST or $_REQUEST variables. But how can I post $_POST to another url let's say post_one.php and access data there?

like image 732
m1k3y3 Avatar asked Nov 28 '22 18:11

m1k3y3


1 Answers

You have to issue a HTTP POST request to your url. A option is to do this using file_get_contents and provide a context. Creating a context is easy using stream_context_create. An example is the following:

$data = http_build_query(
    array(
        'first' => 'your first parameter',
        'second' => 'your second parameter'
    )
);

$headers = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $data
    )
);

$context  = stream_context_create($headers);
$result = file_get_contents($url, false, $context);

This will issue a POST request to $url. $_POST['first'] and $_POST['second'] will be available in your target url.

If you want to re-post all posted variables, replace the first line with:

$data = http_build_query($_POST);
like image 174
alexn Avatar answered Dec 05 '22 15:12

alexn