PHP SOAP不断返回旧结果

时间:2016-04-12 15:39:00

标签: c# php web-services soap websocket

我尝试使用PHP使用SOAP服务时遇到了一个奇怪的问题。它忽略了我的请求,并不断返回最后一个命令的结果! SOAP驻留在收银机中,该机器具有指向公共IP端点的“故障”wsdl,我必须手动编辑它。我下载了http://192.168.1.20/MyEcrResponce.wsdl中的WSDL文件,用'192.168.1.20'替换了所有错误的IP引用,然后我将其保存在本地MyEcrResponce.wsdl:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://192.168.1.20/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://192.168.1.20/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://192.168.1.20/">
      <s:element name="Cmd">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="CmdText"   type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="CmdResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="CmdReply"  type="s:string" />
            <s:element minOccurs="1" maxOccurs="1" name="LcdLine1"  type="s:string" />
            <s:element minOccurs="1" maxOccurs="1" name="LcdLine2"  type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetAppStatus">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="SerialNo"    type="s:string" />
            <s:element minOccurs="1" maxOccurs="1" name="RetData"     type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetAppStatusResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="GetAppStatusResult" type="s:unsignedByte" />
            <s:element minOccurs="1" maxOccurs="1" name="RetData"   type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>    
    </s:schema>
  </wsdl:types>
  <wsdl:message name="CmdSoapIn">
    <wsdl:part name="parameters" element="tns:Cmd" />
  </wsdl:message>
  <wsdl:message name="CmdSoapOut">
    <wsdl:part name="parameters" element="tns:CmdResponse" />
  </wsdl:message>
  <wsdl:message name="GetAppStatusSoapIn">
    <wsdl:part name="parameters" element="tns:GetAppStatus" />
  </wsdl:message>
  <wsdl:message name="GetAppStatusSoapOut">
    <wsdl:part name="parameters" element="tns:GetAppStatusResponse" />
  </wsdl:message>
  <wsdl:portType name="MyEcrResponceSoap">
    <wsdl:operation name="Cmd">
      <wsdl:input message="tns:CmdSoapIn" />
      <wsdl:output message="tns:CmdSoapOut" />
    </wsdl:operation>
    <wsdl:operation name="GetAppStatus">
      <wsdl:input message="tns:GetAppStatusSoapIn" />
      <wsdl:output message="tns:GetAppStatusSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="MyEcrResponceSoap" type="tns:MyEcrResponceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="Cmd">
      <soap:operation soapAction="http://192.168.1.20/Cmd" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetAppStatus">
      <soap:operation soapAction="http://192.168.1.20/GetAppStatus" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>    
  </wsdl:binding>
  <wsdl:service name="MyEcrResponce">
    <wsdl:port name="MyEcrResponceSoap" binding="tns:MyEcrResponceSoap">
      <soap:address location="http://192.168.1.20/MyEcrResponce/" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

我使用表单在C#中创建了一个客户端,将解决方案中的所有引用从公共IP替换为192.168.1.20,并且工作正常:

private void button1_Click(object sender, EventArgs e)
{
    string reply = "";
    string LcdMsg1 = "";
    string LcdMsg2 = "";

    Uri ur = new Uri("http://192.168.1.20/MyEcrResponce.cgx");

    DeviceWS.MyEcrResponceSoapClient DevHD = new DeviceWS.MyEcrResponceSoapClient();
    DevHD.Endpoint.Address = new System.ServiceModel.EndpointAddress(ur);

    DevHD.ClientCredentials.UserName.UserName = "user";
    DevHD.ClientCredentials.UserName.Password = "pass";     

    reply = DevHD.Cmd(CmdTxt.Text, out LcdMsg1, out LcdMsg2);

    ReplyTxt.Text = reply;
    LCD01.Text = LcdMsg1;
    LCD02.Text = LcdMsg2;    
}

我的PHP客户端看起来像这样:

$client = new SoapClient(
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MyEcrResponce.wsdl', 
array('location' => 'http://192.168.1.20/MyEcrResponce.cgx', 
    'login' => 'user', 
    'password' => 'pass', 
    'trace' => 0, 
    'cache_wsdl' => WSDL_CACHE_NONE));  

$cmd = new StdClass();
$cmd->CmdText = 'a/'; 

var_dump($client->Cmd($cmd));

无论我尝试哪种组合,在PHP中我总是得到C#client命令的最后结果。看来所有使用PHP的请求都会被忽略。似乎C#可以跨越某个地方的缓存,但PHP不能。在php.ini中,'soap.wsdl_cache_enabled'和'soap.wsdl_cache'都设置为0.我使用SoapUI和其他SOAP测试人员获得了相同的(错误的)行为。有什么想法吗?

更新1: PHP $ client-&gt; __ getTypes();返回:

array (4) [
    string (31) "struct Cmd {
 string CmdText;
}"
    string (76) "struct CmdResponse {
 string CmdReply;
 string LcdLine1;
 string LcdLine2;
}"
    string (58) "struct GetAppStatus {
 string SerialNo;
 string RetData;
}"
    string (82) "struct GetAppStatusResponse {
 unsignedByte GetAppStatusResult;
 string RetData;
}"
]

更新2:使用this post我在C#客户端启用了跟踪日志,以检查正在运行的SOAP请求:

{
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://192.168.1.20/Cmd"
Accept-Encoding: gzip, deflate,gzip, deflate
Authorization: Basic xxxxxxxxxxxxxxxx
Host: 192.168.1.20
Content-Length: 207
Expect: 100-continue
}

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <Cmd xmlns="http://192.168.1.20/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <CmdText>a/</CmdText>
        </Cmd>
    </s:Body>
</s:Envelope>

不幸的是,当我将请求复制粘贴到PHP或SoapUI时,结果仍然相同。

更新3:所以,今天是星期一。收银机和PHP服务器都在周五关闭,我今天打开了它们。我在运行PHP脚本之前清除了PHP服务器上的所有缓存文件,并且我从周五发送的最后一个成功命令得到了回复!我很接近相信我必须非常愚蠢...

2 个答案:

答案 0 :(得分:0)

试试

ini_set('soap.wsdl_cache_enabled', '0'); 
ini_set('soap.wsdl_cache_ttl', '0');

在创建客户端之前。

我现在在创建客户端时使用NO_CACHE一段时间它会帮助。

答案 1 :(得分:0)

我设法解决了我的问题,至少在某种程度上。我无法使SOAP请求正常工作,但我设法使用websockets成功通信。我坚持使用SOAP因为我有更多的经验,但我不得不放弃。我想要一些非常轻松的东西(我想避免使用基于作曲家的大型图书馆),所以在经过大量搜索后我找到this post,我根据自己的需要量身定制,现在我得到了我的宝贵回复。对于遇到相同或类似问题的其他人,收银机使用的是RBS软件(希腊公司)和Keil-EWeb服务器。我修改过的PHP代码如下:

class WebsocketClient
{
    private $_Socket = null;

    public function __construct($host, $port)
    {
        $header = "GET / HTTP/1.1\r\n";
        $header.= "Host: ".$host.":".$port."\r\n";

        $this->_Socket = fsockopen($host, $port, $errno, $errstr, 2); 
        fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr); 
        $response = fread($this->_Socket, 2000);
    }

    public function __destruct()
    {
        fclose($this->_Socket);
    }

    public function sendData($data)
    {
        fwrite($this->_Socket, $data ) or die('Error:' . $errno . ':' . $errstr); 
        $wsData = fread($this->_Socket, 2000);
        $retData = trim($wsData,"\x00\xff");        
        return mb_convert_encoding($retData,'utf-8','ISO-8859-7'); // Returns garbage in case of greek (or other letters)
    }
}

$WebSocketClient = new WebsocketClient('192.168.1.20', 9101);
echo $WebSocketClient->sendData('a/');
unset($WebSocketClient);