简体   繁体   中英

Synchronising data tables between websites

OK so I have a data table on one site full of ip addresses. I have another site with the same data table, and every week I'd like to sync from site a to site b

So I was thinking of getting the data by getting site b to do a file_get_contents() and call a url on site a, which runs a query and outputs the data by print_r

Is this the best way to do it or is there another way ?

Site B does

$json = file_get_contents('http://sitea.com/?data=synch');

$data = json_decode($json);

var_dump($data);

Site A does this when that URL is called

$db = $this->getConnection();       

$q = "SELECT * FROM data";

$this->_data = $db->recordset( $q );

return json_encode($this->_data);

When I try and get the data at the other end, I can't get it to become an array ?

var_dump($data) just gives me a null?

Here's what I get if I do var_dump($data) on site B

[{"ipID":"1","countryID":"13","beginIP":"1.0.0.0","endIP":"1.0.0.255","netMask":"24","beginIPNum":"16777216","endIPNum":"16777471"},{"ipID":"2","countryID":"44","beginIP":"1.0.1.0","endIP":"1.0.1.255","netMask":"24","beginIPNum":"16777472","endIPNum":"16777727"},{"ipID":"3","countryID":"44","beginIP":"1.0.2.0","endIP":"1.0.3.255","netMask":"23","beginIPNum":"16777728","endIPNum":"16778239"},{"ipID":"4","countryID":"13","beginIP":"1.0.4.0","endIP":"1.0.7.255","netMask":"22","beginIPNum":"16778240","endIPNum":"16779263"},{"ipID":"5","countryID":"44","beginIP":"1.0.8.0","endIP":"1.0.15.255","netMask":"21","beginIPNum":"16779264","endIPNum":"16781311"},{"ipID":"6","countryID":"107","beginIP":"1.0.16.0","endIP":"1.0.31.255","netMask":"20","beginIPNum":"16781312","endIPNum":"16785407"},{"ipID":"7","countryID":"44","beginIP":"1.0.32.0","endIP":"1.0.63.255","netMask":"19","beginIPNum":"16785408","endIPNum":"16793599"},{"ipID":"8","countryID":"107","beginIP":"1.0.64.0","endIP":"1.0.127.255","ne tMask":"18","beginIPNum":"16793600","endIPNum":"16809983"},{"ipID":"9","countryID":"211","beginIP":"1.0.128.0","endIP":"1.0.255.255","netMask":"17","beginIPNum":"16809984","endIPNum":"16842751"},{"ipID":"10","countryID":"44","beginIP":"1.1.0.0","endIP":"1.1.0.255","netMask":"24","beginIPNum":"16842752","endIPNum":"16843007"}]

I've validated it at JSONLint

IF i do $ipRecords = json_decode($data); and then var_dump($ipRecords); I get Null???

Problem solved in chat. Turned out site A was not just outputting the encoded JSON string, so site B needed a way to return just the JSON string from the file_get_contents call.

Solution presented:

On site A:

$db = $this->getConnection();       

$q = "SELECT * FROM data";

$this->_data = $db->recordset( $q );

return "@JSON_OUTPUT@". json_encode($this->_data) ."@JSON_OUTPUT@";

On site B:

$json = explode("@JSON_OUTPUT@", file_get_contents('http://sitea.com/?data=synch'));

$data = json_decode($json[1]);

var_dump($data);

do you have control over both site's ? or is it just web based ? if you have control over both sites, make a mysqldump and pass it (saver). guess its way easyer to work with. and you could run a cronjob to handle this so you wont have to mind anything after setting this up.

you should have a look at this to: Replication

If you have control of both servers replication would work as well. Then you dont have to deal with it the server will do it all for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM