【问题标题】:Send txt file from A domain to B domain And run将txt文件从A域发送到B域并运行
【发布时间】: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


    【解决方案1】:

    将此添加到域 A 的底部。这会通过 php curl 库生成一个发布请求。您需要安装它,而且很可能是这样。 http://php.net/manual/en/book.curl.php

    $ch = curl_init('http://220.xxx.xx');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $array_json);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($array_json))                                                                       
    );  
    

    然后通过将其放在域 B 上来检查来自域 A 的数据:

    var_dump($_RESPONSE);exit;
    

    此外,打开元素检查器上的网络选项卡,以更好地了解正在发生的事情。

    // get from local txt which has been sent to here From other Domain;
    

    它可能会反过来。域 A 不发送文本...域 B 从域 A 检索文本。

    来自 php.net 的示例:http://php.net/manual/en/curl.examples.php

    <?php 
            // create curl resource 
            $ch = curl_init(); 
    
            // set url 
            curl_setopt($ch, CURLOPT_URL, "c://example/sending.txt"); // Domain A
    
            //return the transfer as a string 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    
            // $output contains the output string 
            $output = curl_exec($ch); 
    
            // close curl resource to free up system resources 
            curl_close($ch);      
    
            // or you could use file_get_contents
            $text = file_get_contents('c://example/sending.txt');
            $text_array = json_decode($text); print_r($text_array);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2017-03-23
      • 2012-03-04
      • 2016-01-21
      相关资源
      最近更新 更多