Example of creating and invoking WebService interfaces in PHP

Source: Internet
Author: User
Tags soap wsdl

This article mainly describes the creation and invocation of PHP WebService interface examples, including WebService basic knowledge, WebService server example, WebService client example, the need for 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 know what WebService is. Simply put, WebService is some of the site to open some services, it can be your own development of service, that is, some methods, through the URL, specify a method name, make a request, the site of this service (method), received your request, according to the parameters passed over, do some processing, The processed results are then returned in XML to you, and your program parses the XML data and then displays it or does other things.

Write WebService need to understand: the underlying Web services platform is XML + HTTP, in addition to elements of the Web services Platform: SOAP (Simple Object Access Protocol), UDDI (general description, Discovery, and integration), WSDL (Web Services Description language); Any webservice includes both the client and the server. Here's an example of how to write a 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 is to build directly with the Zend Studio tool, and the other is that PHP automatically generates WSDL files based on SoapDiscovery.class.php, and which one chooses according to their own circumstances, I usually use the former so fast. Here's how to use the class to generate the WSDL file, first need to download the class file on the Internet, and then introduce the class file, look at the following code:
creat_wsdl.php

Copy CodeThe code is as follows: <?php
Include_once (' service.php ');
Include_once (' SoapDiscovery.class.php ');
$WSDL =new soapdiscovery (' service ', ' soap ');//The first parameter is the class name and the name of the generated WSDL service.wsdl, the second parameter is the service name can be written casually
$WSDL->getwsdl ();
?>


This allows the WSDL file to be generated by running the creat_wsdl.php file. Isn't it simple?
Any webservice needs to be bound to an implementation class. In other words, the WSDL file called by others is actually the method of implementing the class, and the following code is the service-side class file
service.php

Copy CodeThe code is as follows: <?php
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 ');//Register All methods of service class
$server->handle ();//Processing request
?>

After the server and WSDL files are written, the client calls are required. See the client calling code:

client.php

Copy CodeThe code is as follows: <?php
Ini_set (' soap.wsdl_cache_enabled ', ' 0 ');//Close cache
$soap =new soapclient (' http://127.0.0.1/soap/Service.php?wsdl ');
echo $soap->add ();
echo $soap->_soapcall (' Add ', array)//or This call can also
?>

This is a complete write WebService interface and the instance code of the call, hope to be helpful to the need of phper;
Then calling someone else's WebService interface is called by the code written by client.php.

Example of creating and invoking WebService interfaces in PHP

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.