Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

how to generate a soap request in php from this xml?

I'm tired of trying to send a request with SOAP. this is my xml

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:bpf="http://schemas.datacontract.org/2004/07/Bpf.Security.Common" xmlns:bpf1="http://schemas.datacontract.org/2004/07/Bpf.Security.Authentication.Common">
         <soapenv:Header>
 <InfoTag xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/BaufestProductivityFramework">
  <ClientIp xmlns="http://schemas.datacontract.org/2004/07/Bpf.Common.Service">200.125.145.10</ClientIp> 
  <CompanyId xmlns="http://schemas.datacontract.org/2004/07/Bpf.Common.Service">1</CompanyId>
  <UserName xmlns="http://schemas.datacontract.org/2004/07/Bpf.Common.Service">someUser</UserName> 
  </InfoTag>
        </soapenv:Header>
           <soapenv:Body>
              <tem:LogIn>         
                <tem:token>
                    <bpf:type>
                       <bpf1:Description>someDesc</bpf1:Description>
                       <bpf1:Id>1</bpf1:Id>
                       <bpf1:Name>someDesc</bpf1:Name>
                    </bpf:type>
                    <bpf:password>somePass</bpf:password>
                    <bpf:userName>someUser</bpf:userName>
                </tem:token>
              </tem:LogIn>
           </soapenv:Body>
        </soapenv:Envelope>

this function send the header with a namespace, but there are more than one... I have to send them all?

 private function __getHeaders() {
            $ns = 'http://schemas.xmlsoap.org/soap/envelope/'; //Namespace of the WS. 
            $ip = $_SERVER['REMOTE_ADDR'];
    //Body of the Soap Header. 
            $headerbody = array('ClientIp' => $ip,
                                'CompanyId' => 1, 
                                'UserName' => 'someUser'
                                );

    //Create Soap Header.        
            $header = new SOAPHeader($ns, 'InfoTag', $headerbody);
            return $header;
        }

    public function prepareWs(){
    $wsdl="the web service";
           $client = new SoapClient($wsdl, array('trace' => true));
    //Set the Headers of Soap Client. 
           $header = $this->__getHeaders();
           $client->__setSoapHeaders($header);

I try to send this body, I inspected exception with soap fault but the message only returns "bad request NULL NULL NULL".

$params = new stdClass();  
      $params = new SoapVar("<tem:token>
        <bpf:type xmlns:bpf="http://schemas.datacontract.org/2004/07/Bpf.Security.Common">
           <bpf1:Description xmlns:bpf1="http://schemas.datacontract.org/2004/07/Bpf.Security.Authentication.Common">someDesc</bpf1:Description>
           <bpf1:Id xmlns:bpf1="http://schemas.datacontract.org/2004/07/Bpf.Security.Authentication.Common">1</bpf1:Id>
           <bpf1:Name xmlns:bpf1="http://schemas.datacontract.org/2004/07/Bpf.Security.Authentication.Common">someName</bpf1:Name>
        </bpf:type>
        <bpf:password xmlns:bpf="http://schemas.datacontract.org/2004/07/Bpf.Security.Common">somePass</bpf:password>
        <bpf:userName xmlns:bpf="http://schemas.datacontract.org/2004/07/Bpf.Security.Common">someUser</bpf:userName>
    </tem:token>", XSD_ANYXML);

       $response = $client->Login($params);

}

With CURL I can send this XML and recieved the XML response too, but with SOAPClient I can't send this request.

I hope someone can help me, thanks.

This is the code I can see with firebug, the only thing I get is "bad request". When I use __getLastRequest() I see the same... I guess the headers should not be sent correctly, however the __setSoapHeaders function returns true. This is the output:

<soap-env:envelope?xmlns:ns1="http://tempuri.org/"?xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header>
<soap-env:contextinformation>
<item>
<key>ClientIp</key>
<value>127.0.0.1</value>
</item>
<item>
<key>CompanyId</key>
<value>1</value>
</item>
<item>
<key>UserName</key>
<value>someUser</value>
</item>
</soap-env:contextinformation>
</soap-env:header>
<soap-env:body>
<tem:login>
<tem:token>
<bpf:type>
<bpf1:description>someDesc</bpf1:description>
<bpf1:id>1</bpf1:id>
<bpf1:name>someName</bpf1:name>
</bpf:type>
<bpf:password>somePass</bpf:password>
<bpf:username>someUser</bpf:username>
</tem:token>
</tem:login>
</soap-env:body>
</soap-env:envelope>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

SoapHeader treats arrays rather arbitrarily. If you ever want to use an array, consider using ArrayObject instead of the native construct.

However, you don't need an array at all since you're only trying to construct a single element in your header. And because each of your internal elements (eg. ClientIP) has a unique namespace, you can't just pass in a basic object. Instead, you have to specify a particular namespace for each element using the SoapVar class, which allows you to wrap normal PHP data in a "SOAP-ready" container that SoapClient can understand and translate.

$innerNS = "http://www.w3.org/BaufestProductivityFramework";
$outerNS = "http://schemas.datacontract.org/2004/07/Bpf.Common.Service";

$tag = new stdClass();
$tag->ClientIP = new SoapVar("200.125.145.10", XSD_STRING, null, null, null, $innerNS);
$tag->CompanyId = new SoapVar(1, XSD_INT, null, null, null, $innerNS);
$tag->UserName = new SoapVar("someUser", XSD_STRING, null, null, null, $innerNS);

$client->__setSoapHeaders(new SoapHeader($outerNS, 'InfoTag', $tag));

Finally, as a rule, don't manually write XML! Consider re-writing your SOAP body code like the header code shown here. You ought to be able to deal specifically with the content of the XML, not its structure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...