Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

xml - soap:Envelope SOAP-ENV:Envelope PHP

I'm trying to login to an API using built-in soap functions of PHP. I got a result like this.

[LoginResult]=> false,
[ErrorMsg] => Login failed with the reason : The security object is invalid

This is what required by the API provider.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
    <Login xmlns="http://tempuri.org/Example/Service1">
          <objSecurity>
              <WebProviderLoginId>test</WebProviderLoginId>
              <WebProviderPassword>test</WebProviderPassword>
              <IsAgent>false</IsAgent>
          </objSecurity>
          <OutPut />
          <ErrorMsg />
    </Login>
</soap:Body>
</soap:Envelope>

&, here is what I was able to produce using functions.

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/Example/Service1">
<SOAP-ENV:Body>
    <ns1:Login>
        <objSecurity>
             <WebProviderLoginId>test</WebProviderLoginId>
             <WebProviderPassword>test</WebProviderPassword>
             <IsAgent>false</IsAgent>
        </objSecurity>
        <OutPut/>
        <ErrorMsg/>
    </ns1:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Here is the code I used to send the request.

<?php
class objSecurity {
function objSecurity($s, $i, $f)   {
    $this->WebProviderLoginId = $s;
    $this->WebProviderPassword = $i;
    $this->IsAgent = $f;
}
}

class nextObject {
function nextObject($objSecurity)   {
    $this->objSecurity=$pobjSecurity;
    $this->OutPut=NULL;
    $this->ErrorMsg=NULL;
}
}

$url    = 'http://example.com/sampleapi/test.asmx?WSDL';
$client = new SoapClient($url, array("soap_version" => SOAP_1_1,"trace" => 1));
$struct = new objSecurity('test', 'test', false);
$data   = new nextObject($struct);
$soapstruct2 = new SoapVar($data, SOAP_ENC_OBJECT);
print_r(
   $client->__soapCall(
       "Login",
       array(new SoapParam($soapstruct2, "inputStruct"))
   )
);

echo $client->__getLastRequest();

?>

These are the differences I found.

In my request xmlns:xsi is missing.

Requirement starts with <soap:Envelope, But my request starts with <SOAP-ENV:Envelope.

There is an extra xmlns:ns1 in my request.

& The function name tag starts with ns1:.

Please help me to make my request into the required format.

I don't know much about the SOAP and I'm using PHP version 5.3.13 with CakePHP 2.3.0. Sorry, for my bad English.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here is the solution. :)

<?php
$url    = 'http://example.com/sampleapi/test.asmx?WSDL';
$client = new SoapClient($url, array("soap_version" => SOAP_1_1,"trace" => 1));

$user_param = array (
  'WebProviderLoginId' => "test",
  'WebProviderPassword' => "test",
  'IsAgent' => false
);

$service_param = array (
  'objSecurity' => $user_param,
  "OutPut" => NULL,
  "ErrorMsg" => NULL
);

print_r(
   $client->__soapCall(
       "Login",
       array($service_param)
   )
);

echo $client->__getLastRequest();

?>

& the request was:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/Example/Service1">
<SOAP-ENV:Body>
   <ns1:Login>
       <ns1:objSecurity>
           <ns1:WebProviderLoginId>test</ns1:WebProviderLoginId>
           <ns1:WebProviderPassword>test</ns1:WebProviderPassword>
           <ns1:IsAgent>false</ns1:IsAgent>
       </ns1:objSecurity>
   </ns1:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Thanks to this link. PHP SOAP Request not right


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...