This article takes the iPhone 6 mobile phone reservation interface development of a company as an example to introduce the implementation process of SOAP call under PHP5.
SOAP (Simple Object Access Protocol) Simple Object Access Protocol is a simple protocol for exchanging information in a decentralized or distributed environment. It is an XML-based protocol. It includes four parts: SOAP envelop (envelop), encapsulation Defines a framework that describes what is in the message, who sent it, who should accept and process it and how to process them; SOAP encoding rules (encoding rules), which are used to represent instances of the data types that the application needs to use; SOAP RPC representation represents the protocol for remote procedure calls and responses; SOAP binding uses the underlying protocol to exchange information.
WSDL (Web Service Description Language) is the standard XML format for describing XML Web services. WSDL was proposed by developers such as Ariba, Intel, IBM and Microsoft. It defines the relevant operations and messages sent and received by a given Web service in an abstract way that is independent of specific languages. By its definition, you cannot yet think of WSDL as an object interface definition language. For example, application architectures such as CORBA or COM will use object interface definition languages. WSDL remains protocol neutral, but it does have built-in support for binding to SOAP, thus establishing an inseparable link with SOAP. So, when I discuss WSDL in this article, I will assume that you use SOAP as your communication protocol.
Although SOAP and WSDL are two major standards for web services, they are not necessarily connected 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.
In the php configuration file php.ini, find
extension=php_soap.dll
Then remove the “;” sign in front, and then restart the web service
The order entry interface of a provincial telecommunications company is http://***.******.com/services/AcceptedBusiness?wsdl
We use SoapClient’s __geunctions() and __getTypes() Method View the methods, parameters and data types of the interface
Only the interfaces listed in __getFunctions can be called by soap.
Create the code soap.php in the root directory
<?<span>php </span><span>header</span>("content-type:text/html;charset=utf-8"<span>); </span><span>try</span><span> { </span><span>$client</span> = <span>new</span> SoapClient("http://***.******.com/services/AcceptedBusiness?wsdl"<span>); </span><span>print_r</span>(<span>$client</span>-><span>__getFunctions()); </span><span>print_r</span>(<span>$client</span>-><span>__getTypes()); } </span><span>catch</span> (SOAPFault <span>$e</span><span>) { </span><span>print</span> <span>$e</span><span>; } </span>?>
After running: http://localhost/soap.php in the browser, the return result is as follows
<span>Array</span><span> ( [</span>0] => ArrayOf_xsd_anyType introduceAcceptedBusiness(<span>string</span> <span>$c3</span>, <span>string</span> <span>$c4</span>, <span>string</span> <span>$linkman</span>, <span>string</span> <span>$linknum</span>, <span>string</span> <span>$num</span>, <span>string</span> <span>$idcard</span>, <span>string</span> <span>$remark</span>, <span>string</span> <span>$address</span><span>) [</span>1] => ArrayOf_xsd_anyType introduceAcceptedBusinessByAiZhuangWei(<span>string</span> <span>$subname</span>, <span>string</span> <span>$linkphone</span>, <span>string</span> <span>$idcard</span>, <span>string</span> <span>$address</span>, <span>string</span> <span>$businesstype</span>, <span>string</span> <span>$marketcode</span>, <span>string</span> <span>$surveycode</span>, <span>string</span> <span>$commanager</span>, <span>string</span> <span>$commanagerphone</span>, <span>string</span> <span>$bendiwang</span>, <span>string</span> <span>$fenju</span>, <span>string</span> <span>$zhiju</span>, <span>string</span> <span>$remark</span><span>) [</span>2] => <span>string</span> <strong>introduceAcceptedBusinessByStandardInterface</strong>(<span>string</span> <span>$xmlStr</span><span>) [</span>3] => <span>string</span> introduceAcceptedBusinessByCallOut(<span>string</span> <span>$xmlStr</span><span>) [</span>4] => <span>string</span> introduceAcceptedBusinessByYddj(<span>string</span> <span>$xmlParam</span><span>) [</span>5] => ArrayOf_xsd_anyType queryAcceptedBusinessByAiZhuangWei(<span>string</span> <span>$surveycode</span>, <span>string</span> <span>$starttime</span>, <span>string</span> <span>$endtime</span><span>) [</span>6] => <span>string</span> queryCallOutOrderByConfig(<span>string</span> <span>$xmlParam</span><span>) ) </span><span>Array</span><span> ( [</span>0] =><span> anyType ArrayOf_xsd_anyType[] )</span>
There is a method introduceAcceptedBusinessByStandardInterface(string $xmlStr), which will be the interface to be used mentioned in the development document, and the parameter is an xml string
In addition, some interfaces mention SoapHeader authentication, which requires adding the __setSoapHeaders method. For details, see http://php.net/manual/zh/soapclient.setsoapheaders.php
This step is to splice the xml string according to the development document, and then pass it in as a parameter of introduceAcceptedBusinessByStandardInterface
Create acceptedbusiness.php with the following content
<?<span>php </span><span>header</span>("content-type:text/html;charset=utf-8"<span>); </span><span>try</span><span> { </span><span>$client</span> = <span>new</span> SoapClient('http://***.*******.com/services/AcceptedBusiness?wsdl'<span>); </span><span>$xml</span> = "<span> <?xml version='1.0' encoding='UTF-8' ?> <PACKAGE> <C3>**电信</C3> <C4></C4> <LINKMAN>张三</LINKMAN> <LINKNUM>13412341234</LINKNUM> <LINKADDRESS>广东深圳</LINKADDRESS> <REMARK>iPhone 6</REMARK> <CHANNEL></CHANNEL> <GRIDCODE>1111111111111111111111111111111</GRIDCODE> <AGENTCODE>2111</AGENTCODE> <KEY>1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</KEY> </PACKAGE> </span>"<span>; </span><span>$return</span> = <span>$client</span>->introduceAcceptedBusinessByStandardInterface(<span>$xml</span><span>); </span><span>print_r</span>(<span>$return</span><span>); } </span><span>catch</span> (SOAPFault <span>$e</span><span>) { </span><span>print_r</span>('Exception:'.<span>$e</span><span>); } </span>?>
After executing in the browser, return
<span><?</span><span>xml version="1.0" encoding="UTF-8"</span><span>?></span> <span><</span><span>PACKAGE</span><span>></span> <span><</span><span>STATUS</span><span>></span>0<span></</span><span>STATUS</span><span>></span> <span><</span><span>REASON</span><span>></span>入单成功!<span></</span><span>REASON</span><span>></span> <span><</span><span>ORDERSEQ</span><span>></span>2014100905523549742<span></</span><span>ORDERSEQ</span><span>></span> <span></</span><span>PACKAGE</span><span>></span>
One of the advantages of soap over nusoap is that it is developed in c and compiled into php internal function library, while NuSOAP is completely written in PHP language and consists of a series of PHP classes. The second advantage is that nusoap existed a long time ago and has stopped updating since 2005-07-27. Soap was added in the php5 version. With php6's support for webservice, I believe that the status of soap as a function library is certain. will keep rising.
The Soap function library of php5 is very convenient to use, and wsdl can be generated using the zend Development Environment development tool.
Note a few issues:
1. In order to improve efficiency, PHP provides a caching function for wsdl files. During development, you can use ini_set("soap.wsdl_cache_enabled", 0); to invalidate it, because the development process It is often necessary to modify wsdl files;
2. SOAP (Simple Object Access Protocol) Simple Object Access Protocol. In php5, it can not only provide the object setClass for remote access calls, but also provide the method addFunctions. So the 'O' in SOAP has been expanded.
3. The server may not be able to get the data POST from the client. This may be a bug in php5 soap functions; the solution is in the server example program below:
if ( isset($HTTP_RAW_POST_DATA)) {
$request = $HTTP_RAW_POST_DATA;
} else {
$request = file_get_contents('php://input');
}
The following is an example program source code.
soap client example:
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);
}
?>
soap server example:
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...the rest of the text>>
There are many types of webservices, and only SOAP-type webservices are called using SoapClient.
In fact, SoapClient is an encapsulation of soap requests. You can also use the underlying interface to access, but this requires you to understand the SOAP protocol and is very troublesome.
There is a library called nusoap, which can easily write SOAP services and SOAP requests. You can learn about it.
Hope to adopt it, thank you for your support!