Misalkan ada 10 situs web yang tersebar di berbagai tempat. Inventarisnya perlu disinkronkan, namun database tidak mendukung koneksi jarak jauh.
Jika kita ingin mendapatkan inventaris server secara real time, kita dapat menggunakan banyak metode. Yang saya tahu adalah sebagai berikut:
·
Metode CURL
·
Metode SOCKET
·
Metode SOAP di PHP5
.php
<?php
$psecode = 'NDE005';
$situs web = 'www.abc.com';
$amt = 1;
$pwd = 123456;
$ch = ikal_init();
$curl_url = " http://ics1.server.com/index.php?web = " $situs web .
"&pwd=" .
"&amt=" .
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Jangan output secara langsung, kembali ke variabel
$curl_result = curl_exec($ch);
$hasil = meledak(',', $curl_result);
curl_close($ch);
print_r($hasil);
?>
Server hanya perlu mengeluarkan dalam format tertentu, dan kemudian klien dapat menerima dalam format ini, seperti:
echo "OK," . $fpsecode "," . $fbalance ;//Pisahkan koma dalam
mode SOCKET
. Ini memerlukan bantuan perpustakaan kelas pihak ketiga HttpClient dapat diunduh di sini: http://scripts.incutio.com/httpclient/
<?php
require_once 'kelas/HttpClient.php';
$params = array('web' => 'www.abc.com',
'pwd' => '123456',
'tindakan' => 'periksa',
'pseid' => 'NDE005',
'amt' => 1);
$pageContents = HttpClient::quickPost('http://ics.server.com/index.php', $params);
$hasil = meledak(',', $pageContents);
print_r($hasil);
?>
Mode SOAP di
server
PHP5.php<?php
fungsi getQuote($fpsecode) {
global $dbh;
$hasil = susunan();
mencoba {
$query = "PILIH fprice, fcansale, fbalance, fbaltip DARI tblbalance di mana upper(trim(fpsecode)) = :psecode limit 1";
$stmt = $dbh->persiapan($query);
$stmt->eksekusi(array(':psecode' => strtoupper(trim($fpsecode))));
$stmt->bindColumn('fprice', $fprice);
$stmt->bindColumn('fcansale', $fcansale);
$stmt->bindColumn('fbalance', $fbalance);
$stmt->bindColumn('fbaltip', $fbaltip);
while($baris = $stmt->mengambil(PDO_FETCH_BOUND)) {
//
}
} tangkapan (PDOException $e) {
gema $e->getMessage();
}
return $fprice; //Anda dapat mengembalikan array
}
$dsn = 'pgsql:host=192.168.*.* port=5432 dbname=db pengguna=123456 kata sandi=123456';
mencoba {
$dbh = PDO baru($dsn);
} tangkapan (PDOException $e) {
die('Koneksi gagal: ' .$e->getMessage());
}
ini_set("soap.wsdl_cache_enabled", "0"); // menonaktifkan cache WSDL
$server = new SoapServer("stockquote.wsdl"); //File konfigurasi
$server->addFunction("getQuote");
$server->pegangan();
?>
stockquote.wsdl
<?xml versi ='1.0' pengkodean ='UTF-8' ?>
<nama definisi='StockQuote'
targetNamespace='http://example.org/StockQuote'
xmlns:tns=' http://example.org/StockQuote '
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<nama pesan='getQuoteRequest'>
<nama bagian='simbol' type='xsd:string'/>
</pesan>
<nama pesan='getQuoteResponse'>
<nama bagian='Hasil' type='xsd:float'/>
</message>
<nama portType='StockQuotePortType'>
<nama operasi='getQuote'>
<masukan pesan='tns:getQuoteRequest'/>
<pesan keluaran='tns:getQuoteResponse'/>
</operasi>
</portType>
<binding name='StockQuoteBinding' type='tns:StockQuotePortType'>
<sabun:gaya pengikatan='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<nama operasi='getQuote'>
<sabun:sabun operasiAction='urn:xmethods-delayed-quotes#getQuote'/>
<masukan>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
codingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</masukan>
<keluaran>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
codingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</keluaran>
</operasi>
</binding>
<nama layanan='StockQuoteService'>
<nama port='StockQuotePort' binding='StockQuoteBinding'>
<sabun:alamat lokasi='http://192.168.3.9/php5/server.php'/>
</pelabuhan>
</layanan>
</definisi>
client.php
<?php
$klien = new SoapClient("stockquote.wsdl");
$hasil = $klien->getQuote("nde005");
print_r($hasil);
?>