PHP - Forward $_POST to another domain

Asked

Viewed 824 times

2

Hello, everybody

I have the following page: "xxx.com.br/pagina1.php"

In this page1.php contains a form whose "action" is set to "yyy.com.br/pagina2.php" (another domain)

I am managing to receive $_POST form variables normally by page 2 of the other domain... But I need to forward one of these variables to "xxx.com.br/pagina3.php" (a third page of the previous domain)

I’m having trouble forwarding a $_POST received to another page from another domain... How do I do this?

  • 1

    Usa cUrl on page 2 to send to page 3.

  • I will use Curl with the example of the friend there, thank you!

1 answer

5


When you get the second POST, use the cURL to send the data you have via POST elsewhere.

<?php
# instancia
$ch = curl_init();
# aqui você transforma seu POST
# você pode fazer um tratamento das variáveis aqui
$meuPost = http_build_query($_POST);
# aqui você informa a url da próxima página
curl_setopt($ch, CURLOPT_URL,"xxx.com.br/pagina3.php");
# aqui você define que é envio via POST
curl_setopt($ch, CURLOPT_POST, 1);
# aqui você pega os dados do post que tinha e reenvia eles
curl_setopt($ch, CURLOPT_POSTFIELDS, $meuPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
# mostra o que aconteceu
print_r($server_output);
  • I need to process the data on page2, forward one of the $_POST to page3 and then redirect to page3 with the $_POST loaded... I place these lines right after data processing?

  • Yes. If you use the example I gave, do your treatment post as needed, then pass the data to the variable $meuPost to happen their treatment in the formed cURL in this example you are using to send the data there.

  • 1

    Okay, I’ll try to do this... Thank you very much!

  • Just one more question... How the "print_r" would work and the page will be redirected and will not give time to print the output?

  • It will not be redirected wanted, it will simulate that redirected to page 3, send the data and stay in the same place. The print_r I put it there just to make sure what’s going on with the execution.

  • But I really needed it to redirect and load the page 3.php with $_POST sent by pagina2

  • Opá, take a look at this post then: PHP Redirect with POST data. But you can run the cURL and then after the execution redirect to the page. Then it will depend on how you are structuring the application.

  • That other question was exactly what I needed. Then I can use: '<form action='xxx.com.br/pagina3.php' method='post' name='frm'> <? php $meuPost = $_POST['fieldA']; ? > <input type='Hidden' name="fieldA" value="<? php echo "$meuPost";? >"/>; </form> <script type="text/javascript"> Document.frm.Submit(); </script>' .?

  • Opá ! Exactly that. (Obs. I think this gambiarra kkk)

  • 1

    Kkkkk no problem, I’m making this "balloon" to another domain for reasons of "I don’t trust the guy q has access to the database of the first domain and I’m handling the data in the database of the second domain that he doesn’t even know exists"... Thank you so much for your help!

  • Opá, we are there for this. A hug.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.