1

我正在尝试创建一个 Web 服务,但我不明白这里有什么问题。

简单客户端.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';
}
?>

简单服务器.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();
?> 

简单视图.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 时,没有显示任何结果。

我正在关注本教程

4

2 回答 2

2

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

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

我从那里下载了代码并粘贴到 Web_Service 目录中的 webroot 目录中。

所以 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();
}
?>
于 2012-10-09T13:59:29.093 回答
0

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

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

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

于 2016-09-10T13:15:12.607 回答