PHP:未捕获的SoapFault异常

时间:2018-03-29 11:38:31

标签: php web-services soap

所以我试图用PHP创建一个SOAP客户端/服务器,但是我收到了以下错误.PS,我知道其他人也问了这个问题并得到了一些回复然后我调查了它们,答案给出了没有修复我的错误,这就是我发布这个问题的原因。

Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document in C:\xamppp\htdocs\C\project\client.php:11 Stack trace: #0 C:\xamppp\htdocs\C\project\client.php(11): SoapClient->__soapCall('getUsersName', Array) #1 C:\xamppp\htdocs\C\project\service.php(5): client->getName(Array) #2 {main} thrown in C:\xamppp\htdocs\C\project\client.php on line 11

这是我的server.php代码:

<?php

class server
{
    private $con;
    public function __construct()
    {
        $this->con=(is_null($this->con)) ? self::connect() : $this->con;
    }
    static function connect()
    {
        $con=mysql_connect('localhost','root','root');
        $db=mysql_select_db('soap',$con);
        return $con;
    }

    public function getUsersName($id)
    {
        $id=trim($id['id']);
        $sql = "SELECT NAME FROM USERS WHERE ID = '$id'";
        $qry = mysql_query($sql, $this->con);
        $res = mysql_fetch_array($qry)
        return $res['name'];
    }
}
$params=array('uri'=>'http://localhost:40/C/project/server.php');
$server= new SoapServer(NULL, $params);
$server->setClass('server');
$server->handle();

?>

这里是client.php:

<?php
class client
{
    public function __construct()
    {
        $params=array('location'=>'http://localhost:40/C/project/server.php','uri'=>'urn//localhost:40/cpu6003/project/server.php','trace'=>1);
        $this->instance= new SoapClient(NULL,$params);
    }
    public function getName($id)
    {
        return $this->instance->__soapCall('getUsersName',$id);
    }
}
$client= new client;
?>

最后,服务:

<?php
include './client.php';
$id=array('id' => '1');

echo $client->getName($id);

?>

1 个答案:

答案 0 :(得分:0)

检查你的服务器日志,server.php可能会抛出一些错误,它会告诉我的错误。

我怀疑它可能是这一行:

$sql = "SELECT NAME FROM USERS WHERE ID = '$id'";

尝试将其更改为以下内容:

$sql = "SELECT NAME FROM USERS WHERE ID = " . (int)$id;