Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/CURL - Making a POST request to another page and following the URL

I am going to try and explain this as best I can, I hope that I make sense as English is not my first language.

Lets say that I have a form on page 1 which contains these 3 inputs:

$_POST["name"];
$_POST["amount"];
$_POST["email"];

And on a second page process.php I also have another input:

$_POST["message"];

And finally on an external website there is a form that I want to POST to without having to type anything into their system:

<form action="display.php" method="post">
 <input type="text" name="name"><br>
 <input type="text" name="amount"><br>
 <input type="text" name="email"><br>
 <input type="text" name="message"><br>
<input type="submit">
</form>

I then want to be able to be automatically redirected to www.example.com/display.php from my process.php page and submit the form automatically and be able to see all of my information that I just entered

Example of what display.php looks like:

Display.php

I do not have access to the code on the external website or display.php so this means that I cannot edit the code or use any sessions.

I hope that you understand me and that you could be able to help me, I would be so grateful if you could help me!

EDIT:

I tested some code that I was given in an answer and looks promising but it didn't pass the POST data to the external page (Which is not external for testing purposes)

Here is the code used to test below:

process.php

<?php
$_POST["name"] = "name";
$_POST["amount"] = "amount";
$_POST["email"] = "email";
$_POST["message"] = "message";

$post = array();
$post[] = "name=" . $_POST["name"];
$post[] = "amount=" . $_POST["amount"];
$post[] = "email=" . $_POST["email"];
$post[] = "message=" . $_POST["message"];

$ch = curl_init('http://localhost/display');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
header('Content-Type: text/html');
echo curl_exec($ch);
?>

display.php

<?php
echo $_POST["name"];
echo $_POST["amount"];
echo $_POST["email"];
echo $_POST["message"];
?>

But for some reason this did not work?

like image 266
rakupu Avatar asked Nov 16 '25 05:11

rakupu


1 Answers

How about this? It assumes name, amount and email will be posted to process.php

Added trouble shooting code.

process.php

 <?php

 //[put database queries here]

 // $message = ??????????????????


$message = 'Message';

$post = array(
'name'=>$_POST['name'],
'amount'=>$_POST['amount'],
'email'=>$_POST['email'],
'message'=>$message);

$ch = curl_init('http://www.example.com/display.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");

header('Content-Type: text/html');
$data = curl_exec($ch);
echo $data;
?>

This was tested and works too

does the redirect to the actual site.

<?php
$name = $_POST["name"];
$amount = $_POST["amount"];
$email = $_POST["email"];
$message = $_POST["message"];

echo <<< EOT
<html><head><style></style></head><body>
<form id="form" action="http://www.example.com/display.php" method="post">
<input type="hidden" name="name" value="$name"/>
<input type="hidden" name="amount" value="$amount"/>
<input type="hidden" name="email" value="$email"/>
<input type="hidden" name="message" value="$message"/>
</form>

<script>document.getElementById("form").submit();</script>
</body></html>
EOT;

?>
like image 187
Misunderstood Avatar answered Nov 17 '25 17:11

Misunderstood