Php webservice writing interface

Source: Internet
Author: User
: This article describes how to write webservice interfaces in php. For more information about PHP tutorials, see. Most APIs written in php are http request-based Apis. sometimes, you can use php to write webservice interfaces in projects.

We need to use php extension soap.

What is soap?

Simple Object Access Protocol
It mainly includes the following four parts:
A) SOAP encapsulation: it is used to encapsulate the content of transmitted data, the message at the sending end, the information at the receiving end, and the processing method to prepare for data transmission.
B) SOAP encoding rules: used to indicate the data types and other information in the transmitted data.
C) SOAP remote process call protocol: protocol used for remote process call and response

1. configure php. ini to open the soap extension: extension = php_soap.dll; remove the previous one;

2. write the server. php. here the data I used for transmission is json

Code:

$ Soap = new SoapServer (null, array ('uri '=> "http: // 192.168.30.120/"); // ip adr
$ Soap-> addFunction ('api _ test ');
$ Soap-> addFunction (SOAP_FUNCTIONS_ALL );
$ Soap-> handle ();
/**
* Api
* @ Param array json
*/
Function api_test ($ num ){
$ Num = json_decode ($ num, 1 );
$ Num ['res'] = $ num ['num1'] + $ num ['num2'];
Return json_encode ($ num, 1 );
}

3. write the client code. json is also used for data transmission:

Code:

/**
* Test php webservice
* @ Param array json
*/
Try {
$ Client = new SoapClient (null,
Array ('location' => "http: // 192.168.30.120/server. php", 'URL' => "http: // 127.0.0.1 /")
);
$ Num ['num1'] = 1;
$ Num ['num2'] = 2;
$ Num = json_encode ($ num, 1 );
$ Rs = $ client-> api_test ($ num );
Echo $ rs;
Echo"

";
print_r(json_decode($rs,1));
echo "
";
} Catch (SoapFault $ fault ){
Echo "Error:", $ fault-> faultcode, ", string:", $ fault-> faultstring;
}
?>

4. enter localhost/client. php in the browser to call the interface and you will get:


Php webservice interface OK!

The above introduces the webservice interface written in php, including some content, and hopes to help friends who are interested in PHP tutorials.

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.