0

I have question and there wasn't answered clearly for me.

Is it possible send $_POST data from domain2.com to domain1.com?

Example:

Script on domain2.com:

<form method="post" action="http://domain2.com">
<input type="hidden" name="data" value="1" />
<input type="submit" value="send" />
</form>

Script on domain1.com:

<?php var_dump($_POST); ?>

The domain2.com is alias of domain1.com and all scripts and data are on domain1.com. I have issue catch the $_POST data.

Any information or links helps me, thank to all.

Maximi
  • 487
  • 5
  • 15

2 Answers2

3
<form method="post" action="">
<input type="hidden" name="data" value="1" />
<input type="submit" value="send" />
</form>  

//extract data from the post extract($_POST);

//set POST variables
$url = 'http://domain2.com/get-post.php';
$fields = array(
                        'data' => urlencode($data),

                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Use this code i am using curl to send post data to domain2

Sameer Shaikh
  • 6,196
  • 2
  • 13
  • 31
  • 1
    I think this is the best solution, but i make it even easier. Redirecting to base domain. Not pretty, but works. In security cases: CSRF, token – Maximi Jan 18 '15 at 19:02
1

Yes it is possible, To do so, you should add this at the top of your PHP file of the receiving domain:

header("access-control-allow-origin: *");
Khoon
  • 2,201
  • 1
  • 16
  • 29