PHP 自身擴展 SOAP 的使用詳解

工做的項目須要使用其餘公司的接口,對方給的接口是Web Service 的soap,以前沒有使用過,這兩天開始研究,瞭解了一個大概: php

php 自身soap: 服務器

1.服務器端:首先完成 service.php  ide

服務器端的主要目的:實現客戶端發送請求的處理類,經過WSDL文件的解釋過程,完成相應的處理。 spa

<?php
/** 
* A simple math utility class 
* @author limx
 */  
class math{  
  /** 
   * Add two integers together 
  * 
  * @param integer $a The first integer of the addition 
  * @param integer $b The second integer of the addition 
  * @return integer The sum of the provided integers 
   */  
  public function add($a, $b) {  
	return $a + $b;  
  }  
   
 /** 
  * Subtract two integers from each other 
  * 
  * @param integer $a The first integer of the subtraction 
  * @param integer $b The second integer of the subtraction 
  * @return integer The difference of the provided integers 
  */  
  public function sub($a, $b) {  
	return $a - $b;  
  }  
   
 /** 
  * Div two integers from each other 
  * 
  * @param integer $a The first integer of the subtraction 
  * @param integer $b The second integer of the subtraction 
  * @return double The difference of the provided integers 
  */  
  public function div($a, $b) {  
	if($b == 0) {  
	  throw new SoapFault(-1, "Cannot divide by zero!");  
	}  
	return $a / $b;  
  }    
}
$server = new SoapServer(math.wsdl', array('soap_version' => SOAP_1_2));  
$server->setClass("math");  
$server->handle();

math.wsdl文件: code

<?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="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
	<message name="add">
		<part name="a" type="xsd:integer"/>
		<part name="b" type="xsd:integer"/>
	</message>
	<message name="addResponse">
		<part name="addReturn" type="xsd:integer"/>
	</message>
	<message name="div">
		<part name="a" type="xsd:integer"/>
		<part name="b" type="xsd:integer"/>
	</message>
	<message name="divResponse">
		<part name="divReturn" type="xsd:double"/>
	</message>
	<message name="getdnstatus">
		<part name="dn" type="xsd:string"/>
	</message>
	<message name="getdnstatusResponse">
		<part name="getdnstatusReturn" type="xsd:string"/>
	</message>
	<message name="sub">
		<part name="a" type="xsd:integer"/>
		<part name="b" type="xsd:integer"/>
	</message>
	<message name="subResponse">
		<part name="subReturn" type="xsd:integer"/>
	</message>
	<portType name="servicePortType">
		<operation name="add">
			<documentation>
				Add two integers together
			</documentation>
			<input message="typens:add"/>
			<output message="typens:addResponse"/>
		</operation>
		<operation name="div">
			<documentation>
				Div two integers from each other
			</documentation>
			<input message="typens:div"/>
			<output message="typens:divResponse"/>
		</operation>
		<operation name="getdnstatus">
			<documentation>
				Getdnstatus one string from each other
			</documentation>
			<input message="typens:getdnstatus"/>
			<output message="typens:getdnstatusResponse"/>
		</operation>
		<operation name="sub">
			<documentation>
				Subtract two integers from each other
			</documentation>
			<input message="typens:sub"/>
			<output message="typens:subResponse"/>
		</operation>
	</portType>
	<binding name="serviceBinding" type="typens:servicePortType">
		<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
		<operation name="add">
			<soap:operation soapAction="urn:serviceAction"/>
			<input>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</input>
			<output>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</output>
		</operation>
		<operation name="div">
			<soap:operation soapAction="urn:serviceAction"/>
			<input>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</input>
			<output>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</output>
		</operation>
		<operation name="getdnstatus">
			<soap:operation soapAction="urn:serviceAction"/>
			<input>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</input>
			<output>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</output>
		</operation>
		<operation name="sub">
			<soap:operation soapAction="urn:serviceAction"/>
			<input>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</input>
			<output>
				<soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
			</output>
		</operation>
	</binding>
	<service name="mathService">
		<port name="servicePort" binding="typens:serviceBinding">
			<soap:address location="http://10.1.7.153/soap/service.php"/>
		</port>
	</service>
</definitions>

2.客戶端:完成服務器端返回值得調用 client.php server

<?php
  //$client = new SoapClient('http://10.1.7.153/soap/math.wsdl');   
  $client = new SoapClient("http://10.1.7.153/soap/service.php?WSDL");  
  try {  
  	$result = $client->div(8,2);
        preint_r($result);
} catch(SoapFault $e) {  
	print "Sorry an error was caught executing your request: {$e->getMessage()}";  
  } 
?>
以上程序的執行結果爲 : 4
相關文章
相關標籤/搜索