在php上创建Web服务

时间:2012-10-09 13:18:29

标签: php web-services web

我正在尝试创建一个Web服务,但我无法理解这里的问题。

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>";
?>

当我打电话给www.example.com/SimpleView.php并将其处理过程输入SimpleClient.php时,没有显示结果。

我关注this tutorial

2 个答案:

答案 0 :(得分:2)

我检查了你提到的教程,它对我有用 只需下载他们给出的代码并更改uri参数 SimpleClient.php文件。

如果您的apache服务器托管在端口80之外,那么请正确指定。

我从那里下载了代码并粘贴在webroot目录里面 Web_Service目录。

所以SimpleClient.php看起来像端口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();
}
?>

如果您的服务器托管在不同的端口,请指定这样的端口。 在这里我提到了端口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();
}
?>

答案 1 :(得分:0)

在SimpleServer中的

:在使用AddHello函数之前,你需要rigester

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

有关Web服务的更多信息并解决您的问题,请查看以下链接: http://www.codeproject.com/Tips/671437/Creating-Web-Service-Using-PHP-Within-Minutes