<p> 假设有10个网站,分布在各地,它们的库存要同步,而数据库不支持远程连接。</p><p> 我们要实时地取得服务器的库存数,可以通过很多种方法,我所知道的有以下几种:</p><p> ・CURL方式</p><p> ・SOCKET方式</p><p> ・PHP5中的SOAP方式</p><p> 以下分别给出示例来实现它:</p><p> CURL方式</p><p> client.php</p><code><?php<br />$psecode = ’NDE005’;<br />$website = ’www.abc.com’;<br />$amt = 1;<br />$pwd = 123456;<br />$ch = curl_init();<br />$curl_url = "http://ics1.server.com/index.php?web=" . $website .<br />"&pwd=" . $pwd . "&action=check&pseid=" . $psecode .<br />"&amt=" . $amt;<br />curl_setopt($ch, CURLOPT_URL, $curl_url);<br />curl_setopt($ch, CURLOPT_POST, 1);<br />curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量<br />$curl_result = curl_exec($ch);<br />$result = explode(’,’, $curl_result);<br />curl_close($ch);<br />print_r($result);<br />?></code></p><p> 服务器端只需按一定的格式输出,然后客户端按此格式接收就可以了如:</p><code>echo "OK," . $fpsecode . "," . $fbalance ;//以逗号分隔</code></p><p> SOCKET方式</p><p> 这个要借助第三方类库HttpClient,可以到这里下载:http://scripts.incutio.com/httpclient/</p><code><?php<br />require_once ’class/HttpClient.php’;<br />$params = array(’web’ => ’www.abc.com’,<br />’pwd’ => ’123456’,<br />’action’ => ’check’,<br />’pseid’ => ’NDE005’,<br />’amt’ => 1);<br />$pageContents = HttpClient::quickPost(’http://ics.server.com/index.php’, $params);<br />$result = explode(’,’, $pageContents);<br />print_r($result);<br />?></code></p>
<p> </p>
<p> PHP5中的SOAP方式</p><p> server.php</p><code><?php<br />function getQuote($fpsecode) {<br />global $dbh;<br />$result = array();<br />try {<br />$query = "SELECT fprice, fcansale, fbalance, fbaltip FROM tblbalance where upper(trim(fpsecode)) = :psecode limit 1";<br />$stmt = $dbh->prepare($query);<br />$stmt->execute(array(’:psecode’ => strtoupper(trim($fpsecode))));<br />$stmt->bindColumn(’fprice’, $fprice);<br />$stmt->bindColumn(’fcansale’, $fcansale);<br />$stmt->bindColumn(’fbalance’, $fbalance);<br />$stmt->bindColumn(’fbaltip’, $fbaltip);<br />while($row = $stmt->fetch(PDO_FETCH_BOUND)) {<br />//<br />}<br />} catch (PDOException $e) {<br />echo $e->getMessage();<br />}<br />return $fprice; //你可以返回一个数组<br />}<br />$dsn = ’pgsql:host=192.168.*.* port=5432 dbname=db user=123456 password=123456’;<br />try {<br />$dbh = new PDO($dsn);<br />} catch (PDOException $e) {<br />die(’Connection failed: ’ . $e->getMessage());<br />}<br />ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache<br />$server = new SoapServer("stockquote.wsdl"); //配置文件<br />$server->addFunction("getQuote");<br />$server->handle();<br />?></code></p><p> stockquote.wsdl</p><code><?xml version =’1.0’ encoding =’UTF-8’ ?><br /><definitions name=’StockQuote’<br />targetNamespace=’http://example.org/StockQuote’<br />xmlns:tns=’ http://example.org/StockQuote ’<br />xmlns:soap=’http://schemas.xmlsoap.org/wsdl/soap/’<br />xmlns:xsd=’http://www.w3.org/2001/XMLSchema’<br />xmlns:soapenc=’http://schemas.xmlsoap.org/soap/encoding/’<br />xmlns:wsdl=’http://schemas.xmlsoap.org/wsdl/’<br />xmlns=’http://schemas.xmlsoap.org/wsdl/’><br /><message name=’getQuoteRequest’><br /><part name=’symbol’ type=’xsd:string’/><br /></message><br /><message name=’getQuoteResponse’><br /><part name=’Result’ type=’xsd:float’/><br /></message><br /><portType name=’StockQuotePortType’><br /><operation name=’getQuote’><br /><input message=’tns:getQuoteRequest’/><br /><output message=’tns:getQuoteResponse’/><br /></operation><br /></portType><br /><binding name=’StockQuoteBinding’ type=’tns:StockQuotePortType’><br /><soap:binding style=’rpc’<br />transport=’http://schemas.xmlsoap.org/soap/http’/><br /><operation name=’getQuote’><br /><soap:operation soapAction=’urn:xmethods-delayed-quotes#getQuote’/><br /><input><br /><soap:body use=’encoded’ namespace=’urn:xmethods-delayed-quotes’<br />encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’/><br /></input><br /><output><br /><soap:body use=’encoded’ namespace=’urn:xmethods-delayed-quotes’<br />encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’/><br /></output><br /></operation><br /></binding><br /><service name=’StockQuoteService’><br /><port name=’StockQuotePort’ binding=’StockQuoteBinding’><br /><soap:address location=’http://192.168.3.9/php5/server.php’/><br /></port><br /></service><br /></definitions><br />client.php<br /><?php<br />$client = new SoapClient("stockquote.wsdl");<br />$result = $client->getQuote("nde005");<br />print_r($result);<br />?></code></p>