歡迎您光臨本站 註冊首頁

php程序與伺服器的通信

←手機掃碼閱讀     火星人 @ 2014-03-24 , reply:0

假設有0個網站,分佈在各地,它們的庫存要同步,而資料庫不支持遠程連接。

  我們要實時地取得伺服器的庫存數,可以通過很多種方法,我所知道的有以下幾種:

   ·CURL方式

   ·SOCKET方式

   ·PHP中的SOAP方式

  以下分別給出示例來實現它:

  CURL方式

  client.php

<?php
$psecode = 』NDE00』;
$website = 』www.abc.com』;
$amt = ;
$pwd = 6;
$ch = curl_init();
$curl_url = "http://ics.server.com/index.php?web=" . $website .
"&pwd=" . $pwd . "&action=check&pseid=" . $psecode .
"&amt=" . $amt;
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, );//不直接輸出,返回到變數
$curl_result = curl_exec($ch);
$result = explode(』,』, $curl_result);
curl_close($ch);
print_r($result);
?>
  伺服器端只需按一定的格式輸出,然後客戶端按此格式接收就可以了如:

echo "OK," . $fpsecode . "," . $fbalance ;//以逗號分隔
  SOCKET方式

  這個要藉助第三方類庫HttpClient,可以到這裡下載:http://scripts.incutio.com/httpclient/

<?php
require_once 』class/HttpClient.php』;
$params = array(』web』 => 』www.abc.com』,
』pwd』 => 』6』,
』action』 => 』check』,
』pseid』 => 』NDE00』,
』amt』 => );
$pageContents = HttpClient::quickPost(』http://ics.server.com/index.php』, $params);
$result = explode(』,』, $pageContents);
print_r($result);
?>
  PHP中的SOAP方式

  server.php

<?php
function getQuote($fpsecode) {
global $dbh;
$result = array();
try {
$query = "SELECT fprice, fcansale, fbalance, fbaltip FROM tblbalance where upper(trim(fpsecode)) = :psecode limit ";
$stmt = $dbh->prepare($query);
$stmt->execute(array(』:psecode』 => 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; //你可以返回一個數組
}

$dsn = 』pgsql:host=9.68.*.* port= dbname=db user=6 password=6』;
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"); //配置文件
$server->addFunction("getQuote");
$server->handle();
?>
  stockquote.wsdl

<?xml version =』.0』 encoding =』UTF-8』 ?>
<definitions name=』StockQuote』
targetNamespace=』http://example.org/StockQuote』
xmlns:tns=』 http://example.org/StockQuote 』
xmlns:soap=』http://schemas.xmlsoap.org/wsdl/soap/』
xmlns:xsd=』http://www.w.org/00/XMLSchema』
xmlns:soapenc=』http://schemas.xmlsoap.org/soap/encoding/』
xmlns:wsdl=』http://schemas.xmlsoap.org/wsdl/』
xmlns=』http://schemas.xmlsoap.org/wsdl/』>

<message name=』getQuoteRequest』>
<part name=』symbol』 type=』xsd:string』/>
</message>
<message name=』getQuoteResponse』>
<part name=』Result』 type=』xsd:float』/>
</message>

<portType name=』StockQuotePortType』>
<operation name=』getQuote』>
<input message=』tns:getQuoteRequest』/>
<output message=』tns:getQuoteResponse』/>
</operation>
</portType>

<binding name=』StockQuoteBinding』 type=』tns:StockQuotePortType』>
<soap:binding style=』rpc』
transport=』http://schemas.xmlsoap.org/soap/http』/>
<operation name=』getQuote』>
<soap:operation soapAction=』urn:xmethods-delayed-quotes#getQuote』/>
<input>
<soap:body use=』encoded』 namespace=』urn:xmethods-delayed-quotes』
encodingStyle=』http://schemas.xmlsoap.org/soap/encoding/』/>
</input>
<output>
<soap:body use=』encoded』 namespace=』urn:xmethods-delayed-quotes』
encodingStyle=』http://schemas.xmlsoap.org/soap/encoding/』/>
</output>
</operation>
</binding>

<service name=』StockQuoteService』>
<port name=』StockQuotePort』 binding=』StockQuoteBinding』>
<soap:address location=』http://9.68..9/php/server.php』/>
</port>
</service>
</definitions>

client.php

<?php
$client = new SoapClient("stockquote.wsdl");
$result = $client->getQuote("nde00");
print_r($result);
?>

[火星人 ] php程序與伺服器的通信已經有301次圍觀

http://coctec.com/docs/linux/show-post-134313.html