2017-06-22 58 views
0

我是擁有一年經驗的初級php開發人員。將A域的txt文件發送到B域並運行

這是我第一次尋求一些幫助

如果有什麼事,是不是合適,請告訴我,非常感謝。

情況:

1.我們有兩個不同的地方(域A,結構域B)

2,當更新在域A上的SQL,保存在JSON類型txt文件,以及(JSON。 .TXT)

3.Then 「發送」從域A此TXT文件域B

4.read和解碼在域B txt文件,則在更新SQL

使用3210

問題:

「發」在情況3是需要幫助的問題。

在這種情況下可以使用什麼樣的方法?

以下是在代碼的全過程:

Domain A = "c://example" 
Domain B = "220.xxx.xx" 
testing file = "sending.txt" 

DomainA中

<?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我svaed

[{"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

但我仍然沒有任何想法。

甚至不知道在哪裏添加我的代碼來測試。

請關注此問題。

非常感謝。

回答

0

將此添加到域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); 
?> 
相關問題