【发布时间】:2017-11-25 03:53:46
【问题描述】:
我是一名初级 php 开发人员,有一年的经验。
这是我第一次寻求帮助
如果有什么不合适的地方,请告诉我,非常感谢。
情况:
1.我们有两个不同的地方(域A,域B)
2.更新域A的sql时,也要保存一个json类型的txt文件。(json.txt)
3.然后“发送”这个txt文件从域A到域B
4.读取并解码域B上的txt文件,然后用于更新sql
问题:
情况 3 中的 “发送” 是需要帮助的问题。
在这种情况下可以使用什么样的方法?
这是代码中的整个过程:
Domain A = "c://example"
Domain B = "220.xxx.xx"
testing file = "sending.txt"
域A
<?php
// this code is on Domain A
include_once "lib/database.php";
$pdo = DB_CONNECT();
$file = "sending.txt";
$f = fopen($file, 'w');
// select data from sql, update and put in array, then save it into txt
$sql = "SELECT id,lastupdated FROM customer";
$pdo -> query($sql);
$rs = $pdo -> query($sql);
foreach ($rs as $key => $row) {
$array[$key]=[
"id" => $row["id"],
"lastupdated" => $row["lastupdated"],
];
$sql = "INSERT INTO customer_test (customer_id,lastupdated) VALUES
(".$row["id"].",'".$row["lastupdated"]."')";
$pdo -> query($sql);
}
$array_json = json_encode($array);
fwrite($f, $array_json);
fclose($f);
?>
我搜索的 Json txt
[{"id":"1","lastupdated":"2017-03-01 13:55:17"},
{"id":"2","lastupdated":"2017-01-08 17:03:39"},
{"id":"3","lastupdated":"2017-02-07 09:34:29"}]
域 B
<?php
include_once "lib/database.php";
$pdo = DB_CONNECT();
// get from local txt which has been sent to here From other Domain;
$json_data = file_get_contents('sending.txt');
$array = json_decode($json_data, true);
//then save into same database,but this one is on Domain B.
foreach ($array as $i => $row) {
$id = $array[$i]["id"];
$lastupdated = $array[$i]["lastupdated"];
$sql = "INSERT INTO customer_test (customer_id,lastupdated) VALUES
(".$id.",'".$lastupdated."')";
$pdo -> query($sql);
}
?>
我应该在这两个php文件中添加什么代码?
我的老板只给我这个链接: How to simulate browser form POST method using PHP/cURL
但我还是一点头绪都没有。
甚至不知道在哪里添加我的代码进行测试。
当您有空时,请查看此问题。
非常感谢。
【问题讨论】:
标签: php json curl cross-domain