This article mainly introduces the creation and invocation of WebService interface in PHP example, including WebService basic knowledge, WebService server example, WebService client example, need friends can refer to the following
As a developer, to write a webservice interface or call someone else's WebService interface, you first need to understand what is webservice. Simply put, WebService is a number of sites open some services, can also be your own development of the service, that is, some methods, through the URL, specify a method name, issued a request, the site of the Service (method), received your request, according to the parameters passed over, do some processing, Then the processed results are returned to you as XML, and your program parses the XML data and then displays it or makes other actions.
Write WebService need to understand: the underlying Web services platform is XML + HTTP; The elements of a Web services platform: SOAP (Simple Object Access Protocol), UDDI (Universal Description, Discovery, and consolidation), WSDL (Web Services Description language); Any webservice includes both the client and the server. Here is an example of how to write the WebService interface with PHP to get someone to call:
First you need to build a. wsdl file, so how does PHP build this file? There are two ways to do this, one that is generated directly with the Zend Studio tool, and the other is that PHP automatically generates WSDL files based on SoapDiscovery.class.php; Here's how to generate a WSDL file with a class, you first need to download that class file online, and then introduce the class file, look at the following code:
creat_wsdl.php
Copy code code as follows:
Include_once (' service.php ');
Include_once (' SoapDiscovery.class.php ');
$WSDL =new soapdiscovery (' service ', ' soap '); The first parameter is the class name and the file name that generated the WSDL service.wsdl, the second parameter is the name of the service can be written casually
$WSDL->getwsdl ();
?>
This allows the WSDL file to be generated by running the creat_wsdl.php file. Is it simple?
Any one of the webservice needs to be bound to an implementation class. In other words, the WSDL file that the other person calls is actually the implementation of the method in the class, the following code is the service-side class file
service.php
Copy code code as follows:
Class Service
{
Public Function Hello ()
{
echo ' Hello good ';
}
Public function Add ($a, $b)
{
return $a + $b;
}
}
$server =soapserver (' service.php ', Array (' Soap_version ' =>soap_1_2));
$server->setclass (' Service ');//All methods of registering service class
$server->handle ();//Processing request
?>
After the server and WSDL files are written, the client calls are required. See Client calling Code:
client.php
Copy code code as follows:
Ini_set (' soap.wsdl_cache_enabled ', ' 0 ');//Close cache
$soap =new soapclient (' http://127.0.0.1/soap/Service.php?wsdl ');
echo $soap->add (1,2);
echo $soap->_soapcall (' Add ', Array (1,2))//or This call can also be
?>