Simple phpwebservicesoap instance

Source: Internet
Author: User
I have been in contact with PHP for many years, but I have never been familiar with webService. Recently, my company wants to use PHP as an interface to consider using webService or directly using HTTP requests, so I have read a lot of information about webService over the past two days.

I have been in contact with PHP for many years, but I have never been familiar with webService. Recently, my company wants to use PHP as an interface to consider using webService or directly using HTTP requests, so I have read a lot of information about webService over the past two days.

Below is a small instance

Action. php file content

 

 
 
  1. //action.php
  2. Class Action {
  3. /**
  4. * Enter description here...
  5. *
  6. * @param int $num1
  7. * @param int $num2
  8. * @return int
  9. */
  10. public function total($num1,$num2){
  11. return $num1 + $num2;
  12. }
  13. }
  14. ?>

 

Service. php file content

 

 
 
  1. //service.php
  2. require './action.php';
  3. $server = new SoapServer('./action.wsdl');
  4. $server->setClass('Action');
  5. $server->handle();
  6. ?>

 

Action. wsdl file content

 

 
 
  1. Xmlns: xsd = "http://www.w3.org/2001/XMLSchema"
  2. Xmlns: soap = "http://schemas.xmlsoap.org/wsdl/soap"
  3. Xmlns: soapenc = "http://schemas.xmlsoap.org/soap/encoding"
  4. Xmlns: wsdl = "http://schemas.xmlsoap.org/wsdl"
  5. Xmlns = "http://schemas.xmlsoap.org/wsdl/">
  6. Enter description here...

 

Index. php file content

 

 
 
  1. //index.php
  2. $test= new SoapClient("http://127.0.0.1/soap2/action.wsdl");
  3. $result = $test->total(22,1);
  4. echo $result;
  5. ?>

 

Here, index. php is the file that calls the interface, and service. php is the service file of the interface. run index. php to directly obtain the value returned by the interface.

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.