SOAP call implementation process in PHP5, and php5soap call Process
This article introduces the implementation process of SOAP calling in PHP5 by taking the iPhone 6 mobile phone reservation interface development of a company as an example.
I. Basic Concepts
Simple Object Access Protocol (SOAP) is a Simple Protocol for exchanging information in a distributed or distributed environment. It is an XML-based Protocol that consists of four parts: the SOAP encapsulation (envelop) defines a framework that describes the content in a message, who sent the message, who should accept and process it, and how to process it; the SOAP encoding rules (encoding rules) are used to represent the Data Type instances required by the application; the soap rpc representation (RPC representation) indicates the protocols for remote process calls and responses; SOAP binding, which uses the underlying protocol to exchange information.
Web Service Description Language (WSDL) is the standard XML format used to describe XML Web Services. WSDL is proposed by developers such as Arba, Intel, IBM, and Microsoft. It defines the operations and messages sent and received by a given Web service in an abstract way unrelated to a specific language. In terms of its definition, you cannot regard WSDL as an object interface definition language. For example, the application architecture such as CORBA or COM will use the object interface definition language. WSDL maintains protocol neutral, but it does have built-in support for binding SOAP, thus establishing an inseparable relationship with SOAP. So when I talk about WSDL in this article, I assume that you use SOAP as your communication protocol.
Although SOAP and WSDL are two standards of web services, they are not necessarily related and can be used independently. The relationship between them is similar to the relationship between HTTP and Html. The former is a protocol, and the latter is a description of a Web Server.
2. PHP5 Configuration
In the php configuration file php. ini, find
extension=php_soap.dll
Remove the previous; number, and then restart the web service.
Iii. Query web service methods, parameters, and Data Types
The incoming ticket interface of a provincial telecommunications company is http: // *****. *******. com/services/AcceptedBusiness? Wsdl
We use the _ geunctions () and _ getTypes () Methods of SoapClient to view the methods, parameters, and Data Types of this interface.
Only interfaces listed in _ getFunctions can be called by soap.
Create Code soap. php In the root directory
<?phpheader("content-type:text/html;charset=utf-8");try { $client = new SoapClient("http://***.******.com/services/AcceptedBusiness?wsdl"); print_r($client->__getFunctions()); print_r($client->__getTypes()); } catch (SOAPFault $e) { print $e;}?>
After you run http: // localhost/soap. php in a browser, the following result is returned:
Array( [0] => ArrayOf_xsd_anyType introduceAcceptedBusiness(string $c3, string $c4, string $linkman, string $linknum, string $num, string $idcard, string $remark, string $address) [1] => ArrayOf_xsd_anyType introduceAcceptedBusinessByAiZhuangWei(string $subname, string $linkphone, string $idcard, string $address, string $businesstype, string $marketcode, string $surveycode, string $commanager, string $commanagerphone, string $bendiwang, string $fenju, string $zhiju, string $remark) [2] => string introduceAcceptedBusinessByStandardInterface(string $xmlStr) [3] => string introduceAcceptedBusinessByCallOut(string $xmlStr) [4] => string introduceAcceptedBusinessByYddj(string $xmlParam) [5] => ArrayOf_xsd_anyType queryAcceptedBusinessByAiZhuangWei(string $surveycode, string $starttime, string $endtime) [6] => string queryCallOutOrderByConfig(string $xmlParam))Array( [0] => anyType ArrayOf_xsd_anyType[])
There is a method introduceAcceptedBusinessByStandardInterface (string $ xmlStr), which will be the interface to be used in the development documentation. The parameter is an xml string.
In addition, some interfaces mentioned SoapHeader authentication, which requires the _ setSoapHeaders method, the specific can view the http://php.net/manual/zh/soapclient.setsoapheaders.php
4. Submit an incoming ticket
This step is to concatenate an xml string according to the development document, and then pass it as the introduceacceptedbusinpolicystandardinterface parameter.
Create acceptedbusiness. php with the following content:
<? Phpheader ("content-type: text/html; charset = UTF-8"); try {$ client = new SoapClient ('HTTP ://***. *******. com/services/AcceptedBusiness? Wsdl '); $ xml = "<? Xml version = '1. 0' encoding = 'utf-8'?> <PACKAGE> <C3> ** Telecom </C3> <C4> </C4> <LINKMAN> Zhang San </LINKMAN> <LINKNUM> 13412341234 </LINKNUM> <LINKADDRESS> Guangdong shenzhen </LINKADDRESS> <REMARK> iPhone 6 </REMARK> <CHANNEL> </CHANNEL> <GRIDCODE> 1111111111111111111111111111111 </GRIDCODE> <AGENTCODE> 2111 </AGENTCODE> <KEY> 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111 </KEY> </PACKAGE> "; $ return = $ client-> introduceAcceptedBusinessByStandardInterface ($ xml); print_r ($ return);} catch (SOAPFault $ e) {print_r ('exception :'. $ e) ;}?>
After execution in the browser, return
<? Xml version = "1.0" encoding = "UTF-8"?> <PACKAGE> <STATUS> 0 </STATUS> <REASON> the ticket is successfully submitted! </REASON> <ORDERSEQ> 2014100905523549742 </ORDERSEQ> </PACKAGE>
What is php webservice?
Compared with nusoap, soap is developed and compiled into a php internal function library in c. NuSOAP is completely written in PHP and consists of a series of PHP classes. Advantage 2: nusoap was available a long time ago. It has stopped updating since, and Soap has been added to php5. with php6's support for webservice, I believe that the status of the function library soap will definitely grow.
The Soap function library of php5 is easy to use. wsdl can be generated using zend Development Environment Development tool.
Notes:
1. to improve efficiency, php provides the cache function for the wsdl file. during development, you can use ini_set ("soap. wsdl_cache_enabled ", 0); make it invalid because the wsdl file is frequently modified during development;
2. Simple Object Access Protocol (SOAP) Simple Object Access Protocol. In php5, you can not only provide the setClass Object for remote Access and call, but also provide the addFunctions method. Therefore, 'O' in SOAP has been extended.
3. The server may not be able to retrieve the data from the client POST, which may be the bugs of php5 soap functions. The solution is provided with a piece of code in the following server example program:
If (isset ($ HTTP_RAW_POST_DATA )){
$ Request = $ HTTP_RAW_POST_DATA;
} Else {
$ Request = file_get_contents ('php: // input ');
}
The following is the sample program source code.
Example of a soap client:
<? Php
Ini_set ("soap. wsdl_cache_enabled", 0 );
Try {
$ Soap = new SoapClient ('authenticate/idolol. wsdl ');
$ Soap-> get_avatar (230 );
$ Functions = $ soap->__ getFunctions ();
Print_r ($ functions );
$ Types = $ soap->__ getTypes ();
Print_r ($ types );
} Catch (SoapFault $ fault ){
Trigger_error ("SOAP Fault: (faultcode: {$ fault-> faultcode}, faultstring: {$ fault-> faultstring})", E_USER_ERROR );
}
?>
Example of the soap server:
<? Php
Require './soap_functions.php ';
Ini_set ("soap. wsdl_cache_enabled", 0 );
$ Server = new SoapServer ('authenticate/idolol. wsdl ', array ('encoding' => 'utf-8 '));
$ Server-> addFunction (array ("user_login", "se ...... full text >>
What other methods does php need to call the webservice function without SoapClient?
There are many types of WebServices. Only WebServices of the SOAP type can be called using SoapClient.
In fact, SoapClient encapsulates soap requests. You can also use the underlying interface for access, but this requires you to understand the SOAP protocol and is very troublesome.
There is a library called nusoap that allows you to easily compile SOAP services and SOAP requests.
Thank you for your support!