开发者

How can I change SOAP Namespaces in PHP to match a WSDL?

I am currently working on a project for the company that I work for. The project will need to make a SOAP request to another company (by means of PHP). I have the SoapClient in my PHP code communicating with the SOAP server of the other company, but all I get is "INVALID XML." After examining their WSDL and the response I notice that their namespaces are different. I need to change my namespaces to match theirs.

For example: The tags in my request show "ns1." and "env.", but theirs show "soap."

How can I change this?

Here is an example of my code, the request that is sent out and the response that is received.

---------------- CODE ----------------

  $client = new SoapClient("http://ws.example.com/test/test.asmx?WSDL", array('soap_version'=>SOAP开发者_Go百科_1_2, 'trace' => 1));

  $result = $client->TestFunction(array('Packet'=>'<Email>test@example.com</Email><Password>examplepassword</Password>'));

  $sessionid = $result->TestFunctionResult;

  print $sessionid;


  echo "REQUEST HEADER:\n" . htmlentities($client->__getLastRequestHeaders()) . "\n";

  echo "REQUEST:\n" . htmlentities($client->__getLastRequest()) . "\n";

  echo "RESPONSE HEADER:\n" . htmlentities($client->__getLastResponseHeaders()) . "\n";

  echo "RESPONSE:\n" . htmlentities($client->__getLastResponse()) . "\n";


------------ REQUEST SENT -------------

POST /test/test.asmx HTTP/1.1
Host: ws.example.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.4
Content-Type: application/soap+xml; charset=utf-8; action="http://ws.example.com/test/TestFunction"
Content-Length: 352

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.example.com/resumes/">
  <env:Body>
    <ns1:TestFunction>
      <ns1:Packet>
        <Email>test@example.com</Email>
        <Password>examplepassword</Password>
      </ns1:Packet>
    </ns1:TestFunction>
  </env:Body>
</env:Envelope>

---------- RESPONSE RECEIVED ----------

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Length: 450
Content-Type: application/soap+xml; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
X-PBY: BEARWS2
Date: Mon, 18 Apr 2011 15:33:51 GMT
Connection: close

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <TestFunctionResponse xmlns="http://ws.example.com/test/">
      <TestFunctionResult>
        <Packet>
          <Error>Invalid XML</Error>
        </Packet>
      </TestFunctionResult>
    </TestFunctionResponse>
  </soap:Body>
</soap:Envelope>

How can I get around this problem?


The prefix names are irrelevant. They are not the cause of your problem. It looks like you have a mismatched tag here:

<ns1:TestFunciton>
    <ns1:Packet>
        <Email>test@example.com</Email>
        <Password>examplepassword</Password>
    </ns1:Packet>
</ns1:TestFunction>

Note the misspelling of TestFunciton.


I wonder why you insert XML tags as values of "Packet". If done correctly (sorry that I cannot check it myself at this time), the SoapClient should escape these as entities, which would destroy your request. Additionally I wonder why your request dump does not show this more correctly, but I assume this might be due to the fact that you were editing stuff instead of copy&paste.

If done correctly, I'd presume, your request should look like this:

$client->TestFunction(
    array(
        'Packet'=> array(
            'Email' => 'test@example.com', 
            'Password' => 'examplepassword',
        )
    )
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜