Example of webservice interface creation and call in php

Source: Internet
Author: User

Example of webservice interface creation and call in php

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:
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, 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
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'); // 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
Copy codeThe Code is as follows: <? Php
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 is a complete example code for writing webservice interfaces and calling. It is helpful for the required phper;
The webservice Interface of others is called as the code written by client. php.

How does php call the web service Interface?

It's not difficult to use soapclient. If you are using java, it should be okay! If you have any questions, please contact us!
Php calls java webservice Interface

The interface can be called directly. It is best to have an interface description. Parameter type is the key

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.