程序与服务器端通讯方法小结呢
2021-08-24 合山装修公司
假设有10个站,分布在各地,它们的库存要同步,而数据库不支持远程连接。我们要实时地取得服务器的库存数,可以通过很多种方法,我所知道的有以下几种: ·CURL方式
·SOCKET方式
·PHP5中的SOAP方式
以下分别给出示例来实现它:
CURL方式
p
<?php
$psecode = ’NDE005’;
$website = ’’;
$amt = 1;
$pwd = 123456;
$ch = curl_init();
$curl_url = \"\" . $website .
\"pwd=\" . $pwd . \"action=checkpseid=\" . $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,可以到这里下载:
<?php
require_once ’class/p’;
$params = array(’web’ => ’’,
’pwd’ => ’123456’,
’action’ => ’check’,
’pseid’ => ’NDE005’,
’amt’ => 1);
$pageContents = HttpClient::quickPost(’
’, $params);
$result = explode(’,’, $pageContents);
print_r($result);
?>
PHP5中的SOAP方式
p
<?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(\"dl_cache_enabled\", \"0\"); // disabling WSDL cache
$server = new SoapServer(\"dl\"); //配置文件
$server->addFunction(\"getQuote\");
$server->handle();
?>
dl
<?xml version =’1.0’ encoding =’UTF-8’ ?>
<definitions name=’StockQuote’
targetNamespace=’’
xmlns:tns=’ ’
xmlns:soap=’’
xmlns:xsd=’’
xmlns:soapenc=’’
xmlns:wsdl=’’
xmlns=’’>
<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=’’/>
<operation name=’getQuote’>
<soap:operation soapAction=’urn:xmethods-delayed-quotes#getQuote’/>
<input>
<soap:body use=’encoded’ namespace=’urn:xmethods-delayed-quotes’
encodingStyle=’’/>
</input>
<output>
<soap:body use=’encoded’ namespace=’urn:xmethods-delayed-quotes’
encodingStyle=’’/>
</output>
</operation>
</binding>
<service name=’StockQuoteService’>
<port name=’StockQuotePort’ binding=’StockQuoteBinding’>
<soap:address location=’’/>
</port>
</service>
</definitions>
p
三星也认可周迅。此外 <?php
$client = new SoapClient(\"dl\");
$result = $client->getQuote(\"nde005\");
print_r($result);
?>
查看本文来源
南昌治男科专科医院小孩脾虚便秘怎么调理
早期类风湿关节炎能治好吗

-
-
海南公租房新办法解决夹心层住房难题搭配
海南公租房新办法解决夹心层住房难题海南中低收入群体、新毕业大学生、新...
- 家居风水 2020-06-06
-
海南公租房新办法解决夹心层住房难题搭配
-
-
核查拉响橙色警报冰冻房企千亿融资冲动搭配
核查拉响“橙色警报”冰冻房企千亿融资冲动去年10月起在地产圈内流传的一...
- 家居风水 2020-06-06
-
核查拉响橙色警报冰冻房企千亿融资冲动搭配
-
-
房地产企业发债融资开展普通商品房建设方案搭配
房地产企业发债融资开展普通商品房建设方案出台在资金使用途径方面,发债...
- 家居风水 2020-06-02
-
房地产企业发债融资开展普通商品房建设方案搭配
-
-
曝诺维茨基已参加数次球队合练啥时候能复出搭配
曝诺维茨基已参加数次球队合练 啥时候能复出?诺维茨基北京时间11月21日消息...
- 家居风水 2020-06-01
-
曝诺维茨基已参加数次球队合练啥时候能复出搭配
-
-
完胜却暴露一环节弱势发球榜单队内最高仅第搭配
完胜却暴露一环节弱势 发球榜单队内最高仅第位中国女排虽完胜泰国却暴露...
- 家居风水 2020-06-01
-
完胜却暴露一环节弱势发球榜单队内最高仅第搭配
-
-
开发商急发高息债外资行收紧楼市拐点真来了搭配
开发商急发高息债外资行收紧 楼市拐点真来了A股市场遭遇“黑色星期一&...
- 家居风水 2020-05-27
-
开发商急发高息债外资行收紧楼市拐点真来了搭配