Suppose there are 10 websites distributed across regions, their inventory needs to be synchronized, and the database does not support remote connection. To obtain the server inventory in real time, we can use many methods. I know the following methods: · CURL method · SOCKET method suppose there are 10 websites distributed in various places, their inventory needs to be synchronized, while the database does not support remote connection.
To obtain the server inventory in real time, we can use the following methods:
· CURL method
· SOCKET method
· SOAP method in PHP5
The following is an example of how to implement it:
CURL method
Client. php
<? Php
$ Response code = 'nde005 ';
$ Website = 'www .abc.com ';
$ Amt = 1;
$ Pwd = 123456;
$ Ch = curl_init ();
$ Curl_url = 'http: // ics1.server.com/index.php? Web = '. $ website.
'& Pwd ='. $ pwd. '& action = check & consumer id ='. $ response code.
'& Amt ='. $ amt;
Curl_setopt ($ ch, CURLOPT_URL, $ curl_url );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // no direct output is returned to the variable
$ Curl_result = curl_exec ($ ch );
$ Result = explode (',', $ curl_result );
Curl_close ($ ch );
Print_ R ($ result );
?>
The server only needs to output data in a certain pattern, and then the client can receive data in this pattern, as shown in the following figure:
Echo 'OK,'. $ fpsecode. ','. $ fbalance; // separated by commas
SOCKET method
The third-party class library HttpClient can be downloaded here: http://scripts.incutio.com/httpclient/
<? Php
Require_once 'class/HttpClient. php ';
$ Params = array ('web' => 'Www .abc.com ',
'Pwd' => '123 ',
'Action' => 'check ',
'Upload ID' => 'nde005 ',
'Amt '=> 1 );
$ PageContents = HttpClient: quickPost ('http: // ics.server.com/index.php', $ params );
$ Result = explode (',', $ pageContents );
Print_r ($ result );
?>
SOAP method in PHP5
Server. php
<? Php
Function getQuote ($ fpsecode ){
Global $ dbh;
$ Result = array ();
Try {
$ Query = 'SELECT fprice, fcansale, fbalance, fbaltip FROM tblbalance where upper (trim (fpsecode) =: your code limit 1 ';
$ Stmt = $ dbh-> prepare ($ query );
$ Stmt-> execute (array (': response code' => strtoupper (trim ($ fpsecode ))));
$ Stmt-> bindColumn ('fprice', $ fprice );
$ Stmt-> bindColumn ('fcansale', $ fcansale );
$ Stmt-> bindColumn ('fbalance ', $ fbalance );
$ Stmt-> bindColumn ('fbaltip ', $ fbaltip );
While ($ row = $ stmt-> fetch (PDO_FETCH_BOUND )){
//
}
} Catch (PDOException $ e ){
Echo $ e-> getMessage ();
}
Return $ fprice; // you can return an array
}
$ Dsn = 'pgsql: host = 192. 168. *. * port = 5432 dbname = db user = 123456 password = 100 ';
Try {
$ Dbh = new PDO ($ dsn );
} Catch (PDOException $ e ){
Die ('connection failed: '. $ e-> getMessage ());
}
Ini_set ('soap. wsdl_cache_enabled ', '0'); // disabling WSDL cache
$ Server = new SoapServer ('stockquote. wsdl '); // configuration file
$ Server-> addFunction ('getquote ');
$ Server-> handle ();
?>
Stockquote. wsdl
<? Xml version = '1. 0' encoding = 'utf-8'?>