首页 文章

从C#访问php soapservice

提问于
浏览
0

好吧,我想做一个简单的web服务,搜索数据库并返回数据,我知道我可以用mysql连接器做到这一点,但这只是为了学习如何使用肥皂这里是PHP肥皂服务器的代码

require_once ('lib/nusoap.php');

$namespace = "http://localhost/webservice/index.php?wsdl";
$server = new soap_server();
$server->configureWSDL("DBQuery");
$server->wsdl->schemaTargetNamespace = $namespace;
$server->register(
    'QueryMsg',
    array('name'=>'xsd:string'),
    array('return'=>'xsd:string'),
    $namespace,
    false,
    'rpc',
    'encoded',
    'returns data from database');

function QueryMsg($query)
{
    $con=mysqli_connect('localhost','root','','webserivce');


    if (mysqli_connect_errno()) {
        return "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    if(!isset($query) or strpos(strtolower($query),'select')<=-1)
    {
        return "invalid order";
    }
    else
    {

        mysqli_real_escape_string($con,$query);
        $result = mysqli_query($con,$query);
                while($row = mysqli_fetch_array($result)) {
                    $data[] = $row;}

                return json_encode($data);

    }
}
// create HTTP listener
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

当我尝试从php soap客户端调用它时,它工作,但当我尝试在Visual Studio中添加此http:// localhost /webservice/index.php作为服务引用从C#应用程序中消耗它我在这里得到一个错误它是

The HTML document does not contain Web service discovery information.
Metadata contains a reference that cannot be resolved: 'http://localhost/webservice/index.php'.
The content type text/xml; charset=ISO-8859-1 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 700 bytes of the response were: '<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode><faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">Operation &apos;&apos; is not defined in the WSDL for this service</faultstring><detail xsi:type="xsd:string"></detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>'.
The remote server returned an error: (500) Internal Server Error.
If the service is defined in the current solution, try building the solution and adding the service reference again.

解决了:实际上很容易有两种方法可以使用WCF并将编码更改为ISO-8859-1或通过在创建soap服务器后添加此行 $server->soap_defencoding = 'UTF-8'; 来更改Web服务本身的编码

1 回答

  • 0

    我会尝试使用像SOAP U.I这样的工具添加服务WSDL . 并看看你从中得到了什么样的错误 . 与使用C#添加Web引用相比,它更加无关紧要,并且可能会披露有关为什么在客户端级别无法使用此内容的更多详细信息 .

    我'm happy to help you troubleshoot this with a little more information. Are you running this service on the same machine where you'重新运行客户端?如果它抱怨无法将文件 http://localhost/webservice/index.php 关联到我想知道发现过程是否正在尝试评估一个可以解决的文件 .

相关问题