PHP SOAP客户端无法正确发送参数。 xsi:nil =“true”而不是value

时间:2012-10-14 15:42:01

标签: php xml arrays web-services soap

我正在尝试使用Web服务但没有成功。看起来xml没有正确设置。

这是我的PHP代码:

<?php

$wsdlUrl = "http://api.clickatell.com/soap/webservice.php?WSDL";
$serviceUrl = "http://api.clickatell.com/soap/webservice.php";

    $request = array("user"=>"anyuser",
                "password"=>"asdsda",
                "api_id"=>"1234"
                );

var_dump($request);

try {
    $client = new SoapClient($wsdlUrl, array("trace" => 1, "location" => $serviceUrl));
    var_dump($client);
    echo "\n";
    $response = $client->auth($request);
    var_dump($response);
    var_dump($client->__getLastRequest());

    echo "\n";
}
catch(Exception $exp) {
    echo "EXCEPTION";
}

?>

SOAP包是:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.clickatell.com/soap/webservice" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:auth>
            <api_id xsi:type="xsd:int">1</api_id>
            <user xsi:nil="true"/>
            <password xsi:nil="true"/>
        </ns1:auth>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

导致api_id,用户和密码未被发送的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

如果您查看var_dump($client->__getFunctions()),您可以看到参数必须像普通函数调用一样传递。

所以你可以用SoapParam

做一个冗长的事情
$response = $client->auth(
    new SoapParam($request["api_id"], "api_id"),
    new SoapParam($request["user"], "user"),
    new SoapParam($request["password"], "password")
);

或者通过直接给出参数来减少冗长:

$response = $client->auth($request["api_id"],
    $request["user"], $request["password"]);