0

I have two php database apps on different servers. I would like to be able to transfer the data from a particular record from the first server to the second, with some processing inbetween to account for the differences in database schema. The two servers cannot access each others databases. So the user would click a link on the record and then basically that data would be transferred over to the other server for confirmation. I looked at a redirect with POST, but that doesn't seem to be possible. Any better ways?

Select record on server 1 -> Process record to correct form -> transfer PHP array to server 2 -> confirmation page on server 2

yoda230
  • 449
  • 6
  • 14
  • if the user is not going to change any data then i suggest you post that data to the other server using curl (Since you cannot connect to the remote database) you can build the http request which will post all the record's fields to the other server and on that end you can write a php code that will insert or update it in the database – AL-Kateb Apr 17 '12 at 10:04
  • I do need the user to confirm the transfer on server 2 (it needs to be compared to see if it exists and eventually an option to update), but I suppose that could be achieved by a redirect after Curl has been successful – yoda230 Apr 17 '12 at 10:21

2 Answers2

1

If I had to do something like this, I'd take my data that needs to be processed, process it on my server (from where I take it) then serialize result and send it to another server using post method. Using another script unserialize and simply store in a database that runs on the second server.

Leri
  • 12,367
  • 7
  • 43
  • 60
  • Yes that is what I had in mind, but how do you "send it to another server using post method". The only way I have found is to create a new page with a hidden form on server 1 and post it using javascript. This though doesn't feel like the right solution to me. – yoda230 Apr 17 '12 at 10:23
1

you can use curl as i mentioned with the help of this example

//assuming the variable that holds your record is $row
$row = array("key1" => "value1", "key2" => "value2");
$post_fields = "";
foreach($row as $key=>$field){
    $post_fields .= $key . "=" . $field . "&";
}
 $Curl_Session = curl_init('http://yourseconddomain.com/yourfile.php');
 curl_setopt ($Curl_Session, CURLOPT_POST, 1);
 curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, $post_fields);
 curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
 curl_exec ($Curl_Session);
 curl_close ($Curl_Session);
AL-Kateb
  • 2,914
  • 3
  • 20
  • 24
  • I think it will be better to serialize it `serialize($post_fields)` and then unserialize instead of sending array. – Leri Apr 17 '12 at 10:37
  • OK that's working well. My only concern would be that the URL as seen by the user is at server1 while showing content that came from server2, but that is not terrible and something I can live with. – yoda230 Apr 17 '12 at 10:49
  • Can u tell how do I parse the data on the receiver end? – provokoe Feb 27 '13 at 11:56
  • On there you just deal with it just like any other data, obviously in the example I'm using the post method so you just use $_POST['key1'] for example – AL-Kateb Mar 03 '13 at 15:45