首页 > PHP中使用Soap出现Function doesn't exist问题,如何解决?

PHP中使用Soap出现Function doesn't exist问题,如何解决?

在下新手,正在用Laravel框架和xampp搭建一个后台管理系统。因为有需要做webservice接口功能,使用到了php中的soap,然而开发过程中出现了Function doesn't exist的问题,卡了好久,因此前来求助。具体如下:

有这么两个php文件:
①client.php,作为soap客户端
路径:C:\xampp\htdocs\my_project\app\Services\client.php

<?php

try {
    $client = new SoapClient(null, array('location' => "http://localhost/my_project/app/Services/server.php", 'uri' => "http://localhost/", 'encoding' => 'UTF-8'));
    echo $client->hello();

} catch (SoapFault $fault) {
    echo "Error: ", $fault->faultcode, ", String: ", $fault->getMessage();
}

②server.php,作为soap服务端
路径:C:\xampp\htdocs\my_project\app\Services\server.php

<?php namespace App\Services;

use App\User; // User是Laravel中的model,在C:\xampp\htdocs\my_project\app下

try {
    $soap = new \SoapServer(null, array('uri' => "localhost"));
    $soap->addFunction('hello');
    $soap->handle();
}catch (\SoapFault $fault) {
    echo "Error: ", $fault->faultcode, ", String: ", $fault->faultstring;
}

function hello()
{
    $model = new User;
    return "hello";
}

因为有需要用到模板,因此在server.php中使用了namespace,并把User类用use的方式引用进来。但是这样一来server.php中的SoapServerSoapFault前面就要加上斜杠,变成\SoapServer\SoapFault

最后在浏览器中运行C:\xampp\htdocs\my_project\app\Services\client.php时却出现了Error: SOAP-ENV:Server, String: Function 'hello' doesn't exist错误提示,百度谷歌一直未果,希望有经验的前辈能指点一下,不胜感激!

【热门文章】
【热门文章】