澳门新葡萄京官网注册php soap 实例

Server端+Client端+WSDL

假设有10个网站,分布在各地,它们的库存要同步,而数据库不支持远程连接。

声明:很简单!!!!!!

  我们要实时地取得服务器的库存数,可以通过很多种方法,我所知道的有以下几种:

参考了如下:

   ?CURL方式

先建Server,然后使用wsdl工具来生成wsdl,我用的是zend development
environment,

   ?SOCKET方式

在zde中的tools中的wsdl generator wizard

   ?PHP5中的SOAP方式

第一页是名字,和输出地址(输出后直接挪过去就OK)

  以下分别给出示例来实现它:

第二页是类和地址,类挑上勾,URL那里添server那个文件的地址

  CURL方式

第三页 finish

  client.php

别忘了拷那个wsdl过去…

<?php
$psecode = ’NDE005’;
$website = ’www.abc.com’;
$amt = 1;
$pwd = 123456;
$ch = curl_init();
$curl_url = “” . $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);
?>

记得server引用的那个类文件里不要有输出.

  服务器端只需按一定的格式输出,然后客户端按此格式接收就可以了如:

一共有两个需要添地址的地方

echo “OK,” . $fpsecode . “,” . $fbalance ;//以逗号分隔

一个是wsdl中的描述

  SOCKET方式

一个是client中的连接

  这个要借助第三方类库HttpClient,可以到这里下载:

类文件

<?php
require_once ’class/HttpClient.php’;
$params = array(’web’ => ’www.abc.com’,
’pwd’ => ’123456’,
’action’ => ’check’,
’pseid’ => ’NDE005’,
’amt’ => 1);
$pageContents = HttpClient::quickPost(’,
$params);
$result = explode(’,’, $pageContents);
print_r($result);
?>

?php

  PHP5中的SOAP方式

/**

  server.php

* @name Culculator.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; //你可以返回一个数组
}

* @date Fri Jan 25 12:43:40 CST 2008

$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();
?>

* @copyright 马永占(MyZ)

  stockquote.wsdl

* @author 马永占(MyZ)

<?xml version =’1.0’ encoding =’UTF-8’ ?>
<definitions name=’StockQuote’
targetNamespace=’
xmlns:tns=’ ’
xmlns:soap=’
xmlns:xsd=’
xmlns:soapenc=’
xmlns:wsdl=’
xmlns=’

* @link

<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>

class Culculator

<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>

/**

client.php

* 求和

<?php
$client = new SoapClient(“stockquote.wsdl”);
$result = $client->getQuote(“nde005”);
print_r($result);
?>

*

* @param float $x

* @param float $y

* @return float

*/

public function add($x, $y)

{

return $x + $y;

}

}

?

Server

?php

/**

* @name CulculatorServer.php

* @date Fri Jan 25 12:44:04 CST 2008

* @copyright 马永占(MyZ)

* @author 马永占(MyZ)

* @link

*/

include(”./Culculator.php”);

$server = new SoapServer(”./Culculator.wsdl”);

$server-setClass(”Culculator”);

$server-handle();

?

Client

?php

/**

* @name CulculatorClient.php

* @date Fri Jan 25 12:43:54 CST 2008

* @copyright 马永占(MyZ)

* @author 马永占(MyZ)

* @link

*/

$soap = new SoapClient(””);

echo $soap-add(1, 2);

?

WSDL

?xml version=”1.0” encoding=”UTF-8”?

!– WSDL file generated by Zend Studio. —

definitions name=math targetNamespace=urn:math xmlns:typens=urn:math
xmlns:xsd= xmlns:soap= xmlns:soapenc= xmlns:wsdl= xmlns=

message name=add

part name=x type=xsd:float/

part name=y type=xsd:float/

/message

message name=addResponse

part name=addReturn type=xsd:float/

/message

portType name=CulculatorPortType

operation name=add

documentation

求和

/documentation

input message=typens:add/

output message=typens:addResponse/

/operation

/portType

binding name=CulculatorBinding type=typens:CulculatorPortType

soap:binding style=rpc transport=

operation name=add

soap:operation soapAction=urn:CulculatorAction/

input

soap:body namespace=urn:math use=encoded encodingStyle=

/input

output

soap:body namespace=urn:math use=encoded encodingStyle=

/output

/operation

/binding

service name=mathService

port name=CulculatorPort binding=typens:CulculatorBinding

soap:address location=

/port

/service

/definitions

发表评论

电子邮件地址不会被公开。 必填项已用*标注