开发者

PHP Soap Client and .NET Web Service

Hi everyone Im trying to consume a .NET with PHP using SoapClient but I got the following issue when my php client send the request, the .NET WS doesnt get the request xml on the right format heres my code i hope some one help me, thanks ind advice

class login {
    public $User;
    public $Password;
}

$logr = new login;

$logr->User = 'user';
$logr->Password = 'pass';


try {
    $client = new soapclient ("http://..../Service.asmx?WSDL", array('classmap' => array('LoginRequest' =&开发者_开发技巧gt; 'login'),));


    print_r($logr);
    $client -> Login ($logr);

}
catch (Exception $e) {
    echo "Error!<br />";
    echo $e -> getMessage ();
}

when i test my .net webserver on a .net application i send this, and it works well

<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/2011/XMLScheme">
    <soap:Body>
        <Login
            xmlns="http://tempuri.org">
            <LoginRequest>
                <User>user</User>
                <Password>pass</Password>
            </LoginRequest>
        </Login
    </soap:Body>
</soap:Envelope>

but when i test it on php i get this, and this error Server was unable to process request. ---> Object reference not set to an instance of an object.

<SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope"
        xmlns:ns1="http://tempuri.org"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        <SOAP-ENV:Body>
            <Login
                xmlns="http://tempuri.com"
                xso:type="ns1:LoginRequest">
                    <User>user</User>
                    <Password>pass</Password>
            </Login>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>


class LoginRequest {
    public $User;
    public $Password;

    public function __construct($usr, $pwd) {
         $this->User = $usr;
         $this->Password = $pwd;
    }
}

$login = new LoginRequest('user', 'password');

$client = new SoapClient('http://..../services.asmx?wsdl');
$client->login($login); // try 1
$client->login(array('LoginRequest' => $login)); //try 2


To use a .Net web service, using the NetBeans IDE, add a 'service' and point to the .net web service (make sure to put ?WSDL at the end), and then drag and drop from the 'service's ' toolbox to a php file and it writes the code for you :)

Works great too.

[I can't say what's wrong with your code though]


I recently ran into this issue as well when helping a customer consume our .Net web service so I thought I would share in case anyone else came across this in the future.

Thanks to adudly for providing guidance that helped shed light on the issue. You have to provide a name for the parameter in the array, and note that the name is case sensitive.

Working Code

try {
    $wsdl_url = 'http://<mywebserver>/LeadWs.svc?wsdl';
    $client = new SOAPClient($wsdl_url);

    $params = array(
        'lead' => ""
    );
    $return = $client->Insert2($params);

    print_r($return);
} catch (Exception $e) {
    echo "Exception occurred: " . $e;
}

My failed attempts used a capital 'L' for Lead. This is apparently the only thing in the WSDLs/XSDs that is lowercase by default. If you do a careful search through the WSDLs/XSDs you will see the exact names of any parameters your method expects. Once you get those right, SoapClient handles the rest of the XML encoding.

My final Code looked like this:

try {
    $wsdl_url = 'http://<mywebserver>/LeadWs.svc?wsdl';
    $client = new SOAPClient($wsdl_url);

    $lead = new Lead(); // Could just be an array as well
                        // but I created a class to help the user
    $lead->FirstName = "Tester";
    $lead->LastName = "Test";
    $lead->ZipCode = "00000";
    $lead->NumberOfTVs = 2;

    $params = array('lead' => $lead);
    $return = $client->Insert2($params);

    print_r($return);
} catch (Exception $e) {
    echo "Exception occurred: " . $e;
}

Hope that helps someone in the future.


You can use SoapHeader and setSoapHeaders here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜