PHP implementation WebService simple examples and implementation steps, webservice example _php tutorial

Source: Internet
Author: User
Tags wsdl

PHP implementation WebService simple examples and implementation steps, webservice example


Some time ago in the webservice of the problem of a long time, originally wanted to write in the framework of thinkphp, but how also can not be realized, so far is only learned without the framework of the interface development.

Share the steps in this resource:

The first files I created are:

api.php API's Interface class file

API.WSDL I create the last interface file to invoke

cometrue.php the execution file for all content that registers the service API class content

creat_wsdl.php creating a WSDL file execution method file

SoapDiscovery.class.php the SOAP class file that must be called (can be queried online for download)

The first step: Create a service folder dedicated to your API interface project

Step two: Download the SoapDiscovery.class.php class file and put it in your service folder

Step three: Create your own API interface class file api.php below the service folder, with examples of file contents as follows:
Copy the Code code as follows:
<?php
Class api{
My Test interface method
Public Function test () {
Return "Hello World";
}
}
?>

OK, after we finish writing the interface, we start to generate a WSDL file on this interface.

Fourth: Write a generated WSDL file execution program, here I will create a new file creat_wsdl.php, also placed under the service sibling directory, the contents are as follows:
Copy the Code code as follows:
<?php
Include ("api.php");
Include ("SoapDiscovery.class.php");
$disc = new Soapdiscovery (' API ', ' service '),//api class file name, service interface directory
$disc->getwsdl ();
?>

Fifth step: Execute the creat_wsdl.php file

Then, under the Service folder, there is a api.wsdl file, as shown in:

It's not the end of the Olympics.

Sixth step: Register all the methods in the API class file, under the Service folder, create a new registration class file named: cometrue.php, the file content is as follows:
Copy the Code code as follows:
<?php
$server = new SoapServer (' api.wsdl ', Array (' soap_version ' = soap_1_2)); # #此处的Service. wsdl file is the one generated above
$server->setclass ("API"); Registering all methods of the service class
$server->handle ();
?>

After the completion of the file execution, this time the class file all the contents of the registration completed, we have to test this interface

Seventh step: In the API class file api.php the bottom, plus the calling program, plus the following api.php file content as follows:
Copy the Code code as follows:
<?php
Class api{
My Test interface method
Public function test ($a) {
return $a;
}
}
$server = new SoapServer (' api.wsdl ', Array (' soap_version ' = soap_1_2)); # #此处的Service. wsdl file is the one generated above
$server->setclass ("API"); Registering all methods of the service class
$server->handle ();
?>

The seventh step: test, in the service outside the arbitrary location (as long as can be accessed) to create a test file named: test.php, the file content is as follows:
Copy the Code code as follows:
<?php
$x = new SoapClient ("http://rbac.local/service/api.php?wsdl"); Here's a link to your own access link
echo $x->test (' OK ');
?>

http://www.bkjia.com/PHPjc/974675.html www.bkjia.com true http://www.bkjia.com/PHPjc/974675.html techarticle PHP Implementation of WebService simple example and implementation steps, webservice example in the previous period of time in the webservice of the problem for a long time, originally wanted to write in the framework of thinkphp, but how ...

  • 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.