Example of creating and calling webservice interfaces in php _ PHP Tutorial

Source: Internet
Author: User
Example of creating and calling a webservice interface in php. Example of creating and calling webservice interfaces in php this article mainly introduces examples of creating and calling webservice interfaces in php, including basic knowledge about webservice, webservice server examples, and webser interfaces created and called in php.

This article mainly introduces examples of creating and calling webservice interfaces in php, including basic knowledge of webservice, webservice server example, and webservice client example. For more information, see

As a developer, to write webservice interfaces or call others' webservice interfaces, you must first understand what webservice is. Simply put, WebService is an open Service for some sites, or a self-developed Service, that is, some methods. you can specify a method name through the URL to send a request, the service (method) in the site receives your request, performs some processing based on the passed parameters, and then returns the processed results to you in XML format, your program parses the XML data and displays it or performs other operations.

To write a webservice, you must understand that the basic Web Services platform is XML + HTTP. In addition, the elements of the Web services platform are SOAP (Simple Object Access Protocol), UDDI (general description, discovery, and integration ), WSDL (Web services Description Language); any webservice includes the client and server. The following example shows how to use php to write webservice interfaces for others to call:

First, we need to create a. wsdl file. how can we create this file in php. There are two ways to achieve this: one is to directly use zend studio tools to generate; the other is php based on SoapDiscovery. class. php automatically generates the wsdl file. Which of the following methods can be selected based on your own situation? I usually use the former method as quickly as possible. Next, let's write how to generate a wsdl file using a class. first, download the class file online, and then introduce the class file. See the following code:


The code is as follows:


Include_once ('service. php ');

Include_once ('soapdiscovery. class. php ');

$ Wsdl = new SoapDiscovery ('service', 'Soap '); // The first parameter is the class name, which is also the name Service of the generated wsdl file. wsdl. The second parameter is the service name.

$ Wsdl-> getWSDL ();


In this way, run the creat_wsdl.php file to generate the wsdl file. Is it easy?

Any webservice must be bound to an implementation class. That is to say, the actual function of the wsdl file called by others is the method in the implementation class. the following code is a server class file.

Service. php

The code is 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'); // registers all methods of the Service class

$ Server-> handle (); // process the request


After writing the server and the wsdl file, the client needs to call it. See the client call code:

Client. php

The code is as follows:


Ini_set ('soap. wsdl_cache_enabled ', '0'); // disable caching

$ Soap = new SoapClient ('http: // php? Wsdl ');

Echo $ soap-> Add (1, 2 );

// Echo $ soap-> _ soapCall ('Add', array (1, 2) // or call


This article describes how to create and call a webservice interface in php, including the basic knowledge of webservice, webservice server example, webser...

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.