PHP程序与服务器端通讯方法小结
时间:2007-02-17 来源:PHP爱好者
假设有10个网站,分布在各地,它们的库存要同步,而数据库不支持远程连接。
我们要实时地取得服务器的库存数,可以通过很多种方法,我所知道的有以下几种:
·CURL方式
·SOCKET方式
·PHP5中的SOAP方式
以下分别给出示例来实现它:
CURL方式
client.PHP
<?PHP
$psecode = ’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&pseid=" . $psecode .
"&amt=" . $amt;
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量
$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’ => ’123456’,
’action’ => ’check’,
’pseid’ => ’NDE005’,
’amt’ => 1);
$pageContents = HttpClient::quickPost(’http://ics.server.com/index.PHP’, $params);
$result = explode(’,’, $pageContents);
print_r($result);
?>
PHP5中的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 1";
$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=192.168.*.* port=5432 dbname=db user=123456 password=123456’;
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 =’1.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.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/’>
<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://192.168.3.9/PHP5/server.PHP’/>
</port>
</service>
</definitions>
client.PHP
<?PHP
$client = new SoapClient("stockquote.wsdl");
$result = $client->getQuote("nde005");
print_r($result);
?>
非常全面的一个php技术网站,php爱好者站 http://www.phpfans.net 有相当丰富的文章和源代码.
我们要实时地取得服务器的库存数,可以通过很多种方法,我所知道的有以下几种:
·CURL方式
·SOCKET方式
·PHP5中的SOAP方式
以下分别给出示例来实现它:
CURL方式
client.PHP
<?PHP
$psecode = ’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&pseid=" . $psecode .
"&amt=" . $amt;
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量
$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’ => ’123456’,
’action’ => ’check’,
’pseid’ => ’NDE005’,
’amt’ => 1);
$pageContents = HttpClient::quickPost(’http://ics.server.com/index.PHP’, $params);
$result = explode(’,’, $pageContents);
print_r($result);
?>
PHP5中的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 1";
$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=192.168.*.* port=5432 dbname=db user=123456 password=123456’;
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 =’1.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.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/’>
<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://192.168.3.9/PHP5/server.PHP’/>
</port>
</service>
</definitions>
client.PHP
<?PHP
$client = new SoapClient("stockquote.wsdl");
$result = $client->getQuote("nde005");
print_r($result);
?>
非常全面的一个php技术网站,php爱好者站 http://www.phpfans.net 有相当丰富的文章和源代码.
相关阅读 更多 +