munibsiddiqui
munibsiddiqui

Reputation: 435

creating web service on php

I am trying to create a web service but I cannot understand what is the problem here.

SimpleClient.php

 <?php
include_once("nusoap.php");

try {

// Create a soap client using SoapClient class
// Set the first parameter as null, because we are operating in non-WSDL mode.
// Pass array containing url and uri of the soap server as second parameter.
$client = new SoapClient(null, array(
'location' => "http://www.example.com/SimpleServer.php",
'uri' => "http://www.example.com"));
// Read request parameter
$param = $_POST['name'];
// Invoke AddHello() method of the soap server (HelloServer)
$result = $client->AddHello($param);
echo $result; // Process the the result
echo "test";
}
catch(SoapFault $ex) {
$ex->getMessage();
echo 'test';
}
?>

SimpleServer.php

    <?php
include_once("nusoap.php");

// Simple Method get 1 parameter and return with Hello
function AddHello($name)
{
     return "Hello $name";
}
// Create SoapServer object using WSDL file.
// For the simplicity, our SoapServer is set to operate in non-WSDL mode. So we do not need a WSDL file
$server = new SoapServer(null, array('uri'=>'http://www.example.com'));
// Add AddHello() function to the SoapServer using addFunction().
$server->addFunction("AddHello");
// To process the request, call handle() method of SoapServer.
$server->handle();
?> 

SimpleView.php

  <?php
echo "<h2>Welcome to PHP Web Service</h2>";
echo "<form action='SimpleClient.php' method='POST'/>";
echo "<input name='name' /><br/>";
echo "<input type='Submit' name='submit' value='Send'/>";
echo "</form>";
?>

No result is shown when I call www.example.com/SimpleView.php and input any word its process to SimpleClient.php.

I am following this tutorial.

Upvotes: 1

Views: 3101

Answers (2)

teodor black
teodor black

Reputation: 3

in SimpleServer: before use AddHello function , you need rigester it

$server->register('AddHello',$name)

for more informtion about web service and solve your problem, check this link : http://www.codeproject.com/Tips/671437/Creating-Web-Service-Using-PHP-Within-Minutes

Upvotes: 0

Uttam Dutta
Uttam Dutta

Reputation: 5310

I checked the tutorial you refered, and it worked for me just download the code they have given and change the uri parameter in SimpleClient.php file.

if your apache server is hosted other then port 80 then specify that properly.

I downloaded the code from there and pasted in webroot directory inside Web_Service directory.

So SimpleClient.php will look like for port 80

<?php
try {
// Create a soap client using SoapClient class
// Set the first parameter as null, because we are operating in non-WSDL mode.
// Pass array containing url and uri of the soap server as second parameter.
$client = new SoapClient(null, array(
'location' => "http://localhost/Web_Service/SimpleServer.php",
'uri' => "http://localhost/Web_Service/"));
// Read request parameter
$param = $_POST['name'];
// Invoke AddHello() method of the soap server (HelloServer)
$result = $client->AddHello($param);
echo $result; // Process the the result
}
catch(SoapFault $ex) {
$ex->getMessage();
}
?>

If your server is hosted in different port then specify the port like this. here I mentioned port 81

<?php
try {
// Create a soap client using SoapClient class
// Set the first parameter as null, because we are operating in non-WSDL mode.
// Pass array containing url and uri of the soap server as second parameter.
$client = new SoapClient(null, array(
'location' => "http://localhost:81/Web_Service/SimpleServer.php",
'uri' => "http://localhost:81/Web_Service/"));
// Read request parameter
$param = $_POST['name'];
// Invoke AddHello() method of the soap server (HelloServer)
$result = $client->AddHello($param);
echo $result; // Process the the result
}
catch(SoapFault $ex) {
$ex->getMessage();
}
?>

Upvotes: 2

Related Questions