Using SOAP in PHP

Source: Internet
Author: User
Tags php server stack trace zend
Soap, English is the meaning of "soap". But this is the thing that has affected the world of the Internet to a great extent, and in the last few years, after the mad "Web service" concept, soap was the achievement or "legacy" because soap was ushered in to implement Web services.
SOAP = Simple Object access Protocol. It is a lightweight, simple, XML-based protocol that is designed to exchange structured and solidified information on the WEB. SOAP can be used in conjunction with many existing Internet protocols and formats, including Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), Multipurpose Internet Mail Expansion Protocol (MIME). It also supports a large number of applications, from the messaging system to Remote procedure calls (RPCs).
The WEB services implemented through the SOAP protocol enable programmers around the world to assemble classes and functions that can be built into a huge library of functions that are language-independent. This for the software business depicts a brilliant development prospects, as long as the network together, you can achieve code-level logical sharing, the past cross-process, cross-machine, cross-network communication problems are all resolved, and the HTTP protocol is able to cross the firewall (in fact, the firewall does not block the HTTP protocol 80 port, Otherwise, no one will be on the Internet).
No wonder many people are very optimistic about the technology, calling it "exciting". Web services are simple to implement and can be easily published based on countless web platforms now on the Internet. Simple is often the most beautiful, Web services is a real example.
In PHP, after the php_soap.dll extension is turned on in the php.ini file, soap can be supported.
In the SOAP extension library, there are three main objects.
1, SoapServer
Defines the functions that can be called and returns the response data when creating a PHP server-side page. The syntax format for creating a SoapServer object is as follows:
$soap = new SoapServer ($wsdl, $array);
Where the WSDL file is used $wsdl for SHOAP, WSDL is a standard format for describing Web service, and if $WSDL is set to NULL, the WSDL pattern is not used. $array is the property information for SoapServer, which is an array.
The AddFunction method of the SoapServer object is used to declare which function can be called by the client, with the following syntax:
$soap->addfunction ($function _name);
Where $soap is a SoapServer object, $function _name is the name of the function that needs to be called.
The handle method of the SoapServer object is used to process the user input and invoke the corresponding function, and finally returns the result to the client for processing. The syntax format is as follows:
$soap->handle ([$soap _request]);
Where $soap is an SoapServer object, $soap _request is an optional parameter that represents the user's request information. If you do not specify $soap_request, it means that the server will receive all requests from the user.
2, Soapcliet
Used to invoke the SoapServer page on the remote server and implement a call to the corresponding function. The syntax format for creating a SoapClient object is as follows:
$soap = new SoapClient ($wsdl, $array);
where parameters $wsdl and $array are the same as soapserver.
After creating the SoapClient object, invoking a function in the service-side page is equivalent to calling the SoapClient method, creating the following syntax:
$soap->user_function ($params);
Where $soap is a SoapClient object, User_function is the function to be called on the server side, $params is the parameter to pass in the function.
3, SoapFault
SoapFault is used to generate errors that may occur during SOAP access. The syntax format for creating a SoapFault object is as follows:
$fault = new SoapFault ($faultcode, $faultstring);
Where $faultcode is a user-defined error code, $faultstring is a user-defined error message. The SoapFault object is generated automatically when an error occurs on the server-side page, or when the user creates the SoapFault object themselves. For errors that occur with SOAP access, the client can obtain the appropriate error message by capturing the Soapfalut object.
After the client captures the SoapFault object, you can obtain the error code and error message by using the following code:
$fault->faultcode;//Error code
$fault->faultstring;//Error message
Where $fault is the SoapFault object that you created earlier.
Example:
File soapfunc.php:
/* Several functions for client-side invocation */
function reverse ($STR)
{
$retval = ";
if (strlen ($STR) <1)
{
return new SoapFault (' Client ', ' ', ' Invalid string ');
}
for ($i =1; $i <=strlen ($STR); $i + +)
{
$retval. = $str [(strlen ($STR)-$i)];
}
return $retval;
}
function Add2numbers ($num 1, $num 2)
{
if (Trim ($num 1)! = Intval ($num 1))
{
return new SoapFault (' Client ', ' ', ' the first number is invalid ');
}
if (Trim ($num 2)! = Intval ($num 2))
{
return new SoapFault (' Client ', ' ', ' the second number is invalid ');
}
Return ($num 1+ $num 2);
}
function gettime ()
{
$time = Date (' y-m-d h:i:s ', Time ());
return $time;
}
?>
File soapclsoapserverient.php content:
Create an instance of the SoapServer object first, then register the function we want to expose,
The last handle () is used to process the accepted SOAP request
Include_once (' soapfunc.php ');
Error_reporting (7); When officially released, set to 0
Date_default_timezone_set (' PRC '); Setting the time zone
$soap = new SoapServer (null, array (' uri ' = "Httr://test-rui"));
$soap->addfunction (' reverse ');
$soap->addfunction (' add2numbers ');
$soap->addfunction (' gettime ');
$soap->addfunction (Soap_functions_all);
$soap->handle ();
?>
File soapclient.php content:
Error_reporting (7);
Try
{
$client = new SoapClient (null, Array (' location ' = ' = ' http://localhost:8080/_myPHP5/soap/soapserver.php ', ' uri ' = = "Http://test-uri"));
$str = "This string would be reversed";
$reversed = $client->reverse ($STR);
echo "If you reverse ' $str ' and you'll get ' $reversed '";
$n 1 = 20;
$n 2 = 33;
$sum = $client->add2numbers ($n 1, $n 2);
echo "
";
echo "If you try $n 1 + $n 2 and you'll get $sum";
echo "
";
echo "The Remoye system time is:". $client->gettime ();
}
catch (SoapFault $fault)
{
echo "fault! Code: ". $fault->faultcode. "String:". $fault->faultstring;
}
?>

The release of the Web service through WSDL is also implemented in PHP.

WSDL is a syntax specification for describing Web services, which is a description document for each Web service, with a detailed description of the location, protocol, and interface of the Web service. Provided by the developer of the Web service.

The WSDL file consists of 5 parts: types, message,porttype,binding, and service five.

1 types Definition: type definition, language Independent. Definition of the element information to be transferred in the SOAP message
2 message: Each Web method corresponds to two message definitions in and out. And the definition of message contains the head and body
3 PortType: Each Web service corresponds to a PortType, and the PortType contains a method to publish it, Operation (operation)
4 Bindings: Specifies the binding information for each operation (class and method) in each porttype, with the format of the message containing input and output.
5 Service: Port information for each Web service binding

WEB Services can also be published through a WSDL document, in addition to being published as described in the previous example.

Example:

To publish the class, file myservice.php:
class Service
{
Public Function HelloWorld ()
{
return "Hello";
}
Public function Add ($a, $b)
{
return $a + $b;
}
}

$server =new soapserver (' testsoap.wsdl ', Array (' soap_version ' = soap_1_2));
$server->setclass ("service");
$server->handle ();
?>

WSDL description document, file testsoap.wsdl:



Targetnamespace= "Urn:testsoap"
Xmlns:typens= "Urn:testsoap"
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/" >

























































Calling code, File client.php:

Error_reporting (7);

$client = new SoapClient ("http://localhost:8080/_myPHP5/soap/Wsdl/TestSoap.wsdl");
echo $client->helloworld ();
Echo ("
");
Echo $client->add (10, 20);
?>

However, the writing of WSDL documents is a very troublesome thing, boring and error prone. Many people think that this thing is not written by people, but if there are good software tools, it is not necessary to write the thing. Zend's ZED 5.0 series and Zend studio for Eclipse 6.0 turned out to be a good support for WDSL visual editing and the release of classes (generated intelligently by a class file), but after Zend Studio 7.0, this functionality was weakened. But the Zend Studio 7.x, built on Eclipse, still has a visual editor for WSDL, and the functionality is also sufficient, and the resulting WSDL file is slightly different from the previous one. Programmers need to be familiar with the tags and elements in the WSDL document.

Appendix: Some errors in PHP development Soap

1, the development time must close the PHP soap cache, the server and the client all need, otherwise will report:

Fatal error:uncaught SoapFault exception: [Client] Function ("test") is a valid method for this service in ... \clien.p Hp:5 Stack Trace:
#0 [Internal function]: Soapclient->__call (' Test ', Array)
#1 D:\xampp\htdocs\clien.php (5): Soapclient->test ()
#2 {main}

Close method:
Ini_set ("soap.wsdl_cache_enabled", "0");

Some information about Soap can be viewed in a way similar to $client->__getfunctions ().


2, if the debugging report does not recognize the XML error, please ensure that there is no extraneous information in the code, such as Utf-8 encoded file BOM header.

Author: Zhang Qing (mesh) Xian PHP Education and Training center 2010-7-11
From "Mesh Horizon": http://blog.why100000.com
Author Micro-Blog: http://t.qq.com/zhangking
"100,000 Why" computer Learning Network: http://www.why100000.com


The above describes the use of SOAP in PHP, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.