Example of installing and using the Soap module in PHP

Source: Internet
Author: User
Tags character set ini soap php server postgresql function web services zts wsdl

The SOAP extension in PHP5 aims to implement PHP's support for Web services. Unlike other methods that implement PHP's support for Web services, SOAP extensions are written in C, so they have a speed advantage over other methods.

SOAP extensions support the following specifications.

* HTTP 1.1
* Http 1.2
* WSDL 1.1

SOAP extensions are mainly used to process RPC Web services. However, you can also use text-format WSDL files to work with the server and client in the WSDL mode.

1. Install the soap module in Linux
After installing php, it is best to keep the files installed at that time, such as usr/local/php-5.3.2
To check whether the soap module is installed, run php-m in the php installation directory.

For example:/usr/local/php/bin/php-m | grep 'Soap'
If no installation is available, go to the php installation source folder.
Cd php-5.3.2/ext/soap
Run the phpize command here.
/Usr/local/php/bin/phpize
Check whether the information has an error. If there is no error, run the following command:
./Configure-with-php-config =/usr/local/php/bin/php-config-enable-soap
Then install and compile
Make
Installation
Make install
After installation, a message is displayed, indicating the path to save the soap. so file.
The compiled soap. so file is saved in the/usr/local/php/lib/php/extensions/no-debug-non-zts-20111222 directory, and then the php. Ini file is modified.
Manually modify: Search for extension_dir = "./" in/usr/local/php/etc/php. ini. It is commented out by default.
Change to extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20111222 /"
Add the following after this line, and then save:
Extension = "soap. so"
Restart apache and run php-m to view the extended soap module.
If you want to install extension modules, you can do so.


II. Install the soap module in Windows


In Windows, you can modify php. ini to select which extension libraries are loaded when PHP is started. You can also use dl () to dynamically load data in the script. All DLL files in the PHP Extension Library have the php _ prefix. Many extension libraries are built into PHP for Windows. This means that no additional DLL files or extension configuration commands are required to load these extension libraries. The PHP Extension Library list in Windows lists the extension libraries that require or have previously required additional php dll files.
To be in php. to enable an extension Library in ini, remove the extension = php _ * line _*. the annotation symbol before the dll. Delete the semicolon (;) before the extension library to be loaded.
Enable php_soap.dll Extension Library
// Set this line
; Extension = php_soap.dll
// Change to this
Extension = php_soap.dll
Note: If you run PHP for the server module version, do not forget to restart the web server after modifying php. ini to make the change take effect.
The following is a list of built-in extension libraries:
Php_bz2.dll bzip2 compression function library
Php_calendar.dll calendar conversion function library
Php_crack.dll password cracking function library
Php_ctype.dll ctype family function library
Php_curl.dll CURL, client URL library function library
Php_dba.dll DBA: Database (dbm style) abstraction layer function library
Php_dbase.dll dBase function library
Php_dbx.dll dbx function library
Php_domxml.dll dom xml function library
Php_dotnet.dll. NET function library
Php_exif.dll EXIF function library
Php_fbsql.dll FrontBase function library
Php_fdf.dll FDF: function library for formatting form data
Php_filepro.dll filePro function library
Php_ftp.dll FTP function library
Php_gd.dll GD library Image function library
Php_gd2.dll GD library Image function library
Php_gettext.dll Gettext function library
Php_hyperwave.dll HyperWave function library
Php_iconv.dll ICONV Character Set Conversion
Php_ifx.dll Informix function library
Php_iisfunc.dll IIS management function library
Php_imap.dll IMAP, POP3 and NNTP function libraries
Php_ingres.dll Ingres II function library
Php_interbase.dll InterBasefunctions
Php_java.dll Java function library
Php_ldap.dll LDAP function library
Php_mbstring.dll multi-byte string function library
Php_mcrypt.dll Mcrypt encryption function library
Php_mhash.dll Mhash function library
Php_mime_magic.dll Mimetype function library
Php_ming.dll Ming function library (Flash)
Php_msql.dll mSQL function library
Php_mssql.dll MSSQL function library
Php_mysql.dll MySQL function library
Php_mysqli.dll MySQLi function library
Php_oci8.dll Oracle 8 function library
Php_openssl.dll OpenSSL function library
Php_overload.dll object overload function library
Php_pdf.dll PDF function library
Php_pgsql.dll PostgreSQL function library
Php_printer.dll printer function library
Php_shmop.dll shared memory function library
Php_snmp.dll SNMP function library
Php_soap.dll SOAP function library
Php_sockets.dll Socket function library
Php_sybase_ct.dll Sybase function library
Php_tidy.dll Tidy function library
Php_tokenizer.dll Tokenizer function library
Php_w32api.dll W32api function library
Php_xmlrpc.dll function library for XML-RPC
Php_xslt.dll XSLT function library
Php_yaz.dll YAZ function library
Php_zip.dll Zip file function library
Php_zlib.dll ZLib compression function library


How to use the SOAP module


First, let's take a look at the three main objects of SOAP extensions:
1. SoapServer

The code is as follows: Copy code

SoapServer is used to define the functions that can be called and return response data when creating a php server page. The syntax format for creating a SoapServer object in NON-WSDL mode is as follows:
$ Soap = new SoapServer (null, $ arr );

$ Arr is the property information of SoapServer and an array.
The addFunction method of the SoapServer object is used to declare which function can be called by the client. The method is as follows:
$ Soap-> addFunction ($ functionName );

$ FunctionName is the name of the method that can be called by the client. If all methods can be called, use the following method:
$ Soap-> addFunction (SOAP_FUNCTIONS_ALL );

The handle method of the SoapServer object is used to process user input, call the corresponding function, and finally return the result to the client for processing. The usage is as follows:
$ Soap-> handle ([$ soapRequest]);

$ SoapRequest is an optional parameter used to indicate the user's request information. If $ soapRequest is not specified, the server receives all user requests.
2. SoapClient

SoapClient is used to call the SoapServer page on the remote server and call the corresponding function. The method for creating a SoapClient object is as follows:

The code is as follows: Copy code
$ Client = new SoapClient (null, $ arr );

The $ arr parameter is the same as the SoapServer parameter. After the SoapClient object is mounted, the method for calling the server side is as follows:

The code is as follows: Copy code
$ Client-> functionName ($ p );

FunctionName () is the name of the function to be called on the server, and $ p is the parameter.
3. SoapFault

SoapFault is used to generate possible errors during soap access. The method for creating a SoapFault object is:

The code is as follows: Copy code
$ Fault = new SoapFault ($ code, $ msg );

The parameter $ code is the user-defined error code, and $ msg is the user-defined error message. The soapFault object is automatically generated when an error occurs on the server page or when you create a SoapFault object. For errors during Soap access, the client can capture the SoapFalut object to obtain the corresponding error information. After capturing the SoapFault object on the client, you can use the following code to obtain the error code and error message:

The code is as follows: Copy code

$ Fault-> code; // error code
$ Fault-> msg; // error message

The above is the introduction of three main objects of the php soap module. Here we will use a simple example to help you understand how to use the soap module.
4. php soap instance (NON-WSDL mode)

Server code server. php:

The code is as follows: Copy code

<? Php
$ Arr = array (
'Location' => 'http: // 192.168.187.132/server. Php ',
'URL' => 'http: // 192.168.187.132 /'
);
$ Soap = new SoapServer (null, $ arr );
   
$ Soap-> addFunction (SOAP_FUNCTIONS_ALL); // allows the client to call all methods
$ Soap-> handle ();


Function (){
Return 'Lee's blog ';
    }  
Function B ($ B ){
Return $ B;
    }  

           

Client code:

The code is as follows: Copy code

<? Php
$ Arr = array (
'Location' => 'http: // 192.168.187.132/server. Php ',
'URL' => 'http: // 192.168.187.132 /'
);
$ Soap = new SoapClient (null, $ arr );
   
Echo $ soap-> a (), '<br/> ';
Echo $ soap-> B ('B ');
?>

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.