nuSoap didn't show the expected result

110 views Asked by At

I'm a beginner and still learning.

I have a nusoap problem. I am trying to find out what is wrong with my code because I think all of it is correct based on all the tutorial that I've searched. But still when I run the client.php it did not show the result from my server.php

PHP version PHP/8.1.6

nusoap version NuSOAP/0.9.11

This is my server.php

<?php
 
// mengincludekan file berisi class nusoap
require_once 'nusoap.php';


// instansiasi class soap untuk server
$server = new soap_server();
$server->configureWSDL('server', 'urn:server');


// meregistrasi 'method' untuk proses penjumlahan dengan nama 'jumlahkan'
$server->register('jumlahkan');

 
// detil isi method 'jumlahkan'
function jumlahkan($x, $y) {
    return 'jumlahkan' . $x + $y ;
}
 
// memberikan response service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA: '';
$server->service(file_get_contents('php://input'));

?>

and this is my client.php

<?php
 
require_once 'nusoap.php';

$wsdl = "http://localhost/WS/nusoap/server.php?wsdl";

// instansiasi obyek untuk class nusoap client
$client = new nusoap_client($wsdl, 'wsdl');

$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = FALSE;

$err = $client->getError();

if($err){
echo 'Error'.$err;
}
 
// dua bilangan yang akan dijumlahkan atau dikurangi
$bil1 = 10;
$bil2 = 25;

// proses call method 'jumlahkan' di script server.php yang ada di komputer B
$result = $client->call('jumlahkan', array('x' => $bil1, 'y' => $bil2));

echo "<p>Hasil penjumlahan ".$bil1." dan ".$bil2." adalah ".$result."</p>";
//print_r($result); 
 
 
// menampilkan format XML hasil response
echo '<h2>Respond</h2>';
echo '<pre>'.$client->response.'</pre>';

echo '<h2>Request</h2>';
echo '<pre>'.$client->request.'</pre>';
 
?>

Error

Fatal error:  Uncaught ArgumentCountError: Too few arguments to function jumlahkan(), 0 passed in D:\xampp\htdocs\WS\nusoap\nusoap.php on line 4204 and exactly 2 expected in D:\xampp\htdocs\WS\nusoap\server.php:17
Stack trace:
#0 D:\xampp\htdocs\WS\nusoap\nusoap.php(4204): jumlahkan()
#1 D:\xampp\htdocs\WS\nusoap\nusoap.php(3825): nusoap_server->invoke_method()
#2 D:\xampp\htdocs\WS\nusoap\server.php(26): nusoap_server->service('<?xml version="...')
#3 {main}
  thrown in D:\xampp\htdocs\WS\nusoap\server.php on line 17
1

There are 1 answers

9
hanif zekri On

I rewrite your code and fix some problems. I test the result, and it's worked fine at my end. You can compare blow code with yours, so you can see what's wrong in yours.

server.php

<?php

ini_set('display_errors', 1);

// mengincludekan file berisi class nusoap
require_once 'nusoap.php';


// instansiasi class soap untuk server
$server = new soap_server();
$server->configureWSDL('server', 'urn:server');

// meregistrasi 'method' untuk proses penjumlahan dengan nama 'jumlahkan'
$server->register('jumlahkan', array('x' => 'xsd:int', 'y' => 'xsd:int'), array('return' => 'xsd:int'));

// detil isi method 'jumlahkan'
function jumlahkan($x, $y) {
    return ($x + $y);
}
 
// memberikan response service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA: '';
$server->service(file_get_contents('php://input'));

?>

client.php

<?php

ini_set('display_errors', 1);
require_once 'nusoap.php';

$wsdl = "http://localhost/WS/nusoap/server.php?wsdl";

// instansiasi obyek untuk class nusoap client
$client = new nusoap_client($wsdl, 'wsdl');

$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = FALSE;

$err = $client->getError();

if($err){
echo 'Error'.$err;
}
 
// dua bilangan yang akan dijumlahkan atau dikurangi
$bil1 = 10;
$bil2 = 25;

// proses call method 'jumlahkan' di script server.php yang ada di komputer B
$result = $client->call('jumlahkan', array($bil1, $bil2));
echo "<p>Hasil penjumlahan ".$bil1." dan ".$bil2." adalah ".$result."</p>";
//print_r($result);
 
// menampilkan format XML hasil response
echo '<h2>Respond</h2>';
echo '<pre>'.$client->response.'</pre>';

echo '<h2>Request</h2>';
echo '<pre>'.$client->request.'</pre>';

?>