To create SOAP client in CodeIgniter, it is better to use NuSOAP (SOAP Toolkit for PHP).
Download NuSOAP Toolkit from this URL: Download
Below are the steps to create SOAP client in CodeIgniter using NuSOAP PHP Toolkit.
Step 1: After downloading NuSOAP toolkit, copy “lib” and “nusoap” folder in “application/library/” folder.
Step 2: Create one library file say “nusoap_library.php” in “application/library” folder and copy and paste below given code in “application/library/nusoap_library.php” file. It just includes “nusoap.php” library file from “lib” folder.
1
2
3
4
5
6
7
| class Nusoap_library { function Nusoap_library() { require_once ( 'lib/nusoap' .EXT); } } |
Step 3: SOAP request function. Copy and paste below given function to “application/library/nusoap_library.php” file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| function soaprequest( $api_url , $api_username , $api_password , $service , $params ) { if ( $api_url != '' && $service != '' && count ( $params ) > 0) { $wsdl = $api_url . "?wsdl" ; $client = new nusoap_client( $wsdl , 'wsdl' ); $client ->setCredentials( $api_username , $api_password ); $error = $client ->getError(); if ( $error ) { echo "\nSOAP Error\n" . $error . "\n" ; return false; } else { $result = $client ->call( $service , $params ); if ( $client ->fault) { print_r( $result ); return false; } else { $result_arr = json_decode( $result , true); $return_array = $result_arr [ 'result' ]; return $return_array ; } } } } |
Step 4: To send SOAP request to SOAP web service method, we need to load NuSOAP library in Controller where we want to use SOAP client.
1
| $this ->load->library( "Nusoap_lib" , "" ); |
Step 5: SOAP service call in Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
| $api_username = AUTHENTICATION USERNAME; $api_password = AUTHENTICATION PASSWORD; $service = "addnumbers" ; // from my POST <a href="http://www.dharamart.in/2013/soap-server-in-codeigniter-using-nusoap-library/" target="_blank">SOAP Server In CodeIgniter using NuSOAP PHP Toolkit</a> $params = array ( 'a' => 5, 'b' => 10); // input parameters to "addnumbers" medhod if ( $api_url != '' ) { $result = $this ->nusoap_lib->soaprequest( $api_url , $api_username , $api_password , trim( $service ), $params ); if ( is_array ( $result ) && count ( $result ) > 0) { print_r( $result ); } } |
Hope this post will help you !!!
No comments:
Post a Comment