2017-06-22 7 views
0

私は1年間の経験を持つジュニアPHP開発者です。AドメインからBドメインにtxtファイルを送信して実行してください

それが適切でないものがあれば、いくつかの助け

を求めるのは初めてだ、おかげで多くのことを教えてください。

状況:

1.Weは、ドメインAのSQLを更新する2つの異なる場所(ドメインA、ドメインB)

たら、2.を持っているだけでなくJSONタイプでtxtファイルを保存する(JSON。 .TXT)

3.Then 次にSQL

の更新に使用される、ドメインBに

4.readをドメインAからこのtxtファイルを "送信" とドメインB上のtxtファイルを復号3210

質問:

状況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 Iは

[{"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); 
} 
?> 
をsvaed TXT

これらの2つの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はphp.netからドメインA.

例からテキストを取得します。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); 
?> 
関連する問題