简体   繁体   中英

how to insert params to soap api - php soap

I would like to connect with API - send request and get data.

First, I get all the available functions:

$client = new SoapClient('https://website.com/api.asmx?wsdl');
print_r($client->__getFunctions());

And I display array, like this:

[0] => CheckExampleResponse CheckExample(CheckExample $parameters)

So, I must use this way:

$client->CheckExample($params);

but, I don't know how to send this XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="XmlServices">
    <soapenv:Header/>
    <soapenv:Body>
        <xs:GetExample>
            <xs:request>
                <xs:Credentials>
                    <xs:UserName>?</xs:UserName>
                    <xs:Password>?</xs:Password>
                    <xs:PostlId>?</xs:HotelId>
                </xs:Credentials>
                <xs:DataTest>2015-01-01T00:00:00</xs:DataTest>
                <xs:DataTest2>2015-01-02T23:59:59</xs:DataTest2>
                <xs:StatusTest>publish</xs:StatusTest>
            </xs:request>
        </xs:GetExample>
    </soapenv:Body>
</soapenv:Envelope>

When I pass this XML string as an argument to CheckExample , I get this error: "Request parameter cannot be null."

Thanks.

The GetExample method should also appear in the __getFunctions call. Then this should work:

<?php
$client = new SoapClient('https://website.com/api.asmx?wsdl');

$soapmessage = [
    'request' => [
        'Credentials' => [
            'UserName' => '?',
            'Password' => '?',
            'PostlId' => '?'
        ],
        'DataTest' => '2015-01-01T00:00:00',
        'DataTest2' => '2015-01-02T23:59:59',
    ]
];

$result = $client->GetExample($soapmessage);
print_r($result);

When you call a SOAP function as a method of SoapClient , you are essentially calling the SoapClient::__soapCall() function, and passing the function name as the first parameter. So, it's good to understand the parameters for this function.

You can see that to pass the arguments, you need to pass an array. This must match what the SOAP server is expecting; the details can be found in the WSDL, and are not displayed by the call to SoapClient::__getFunctions() . The SOAP service provider should also provide API documentation to determine the proper values, if you don't want to read the WSDL.

It's worth remembering that even if a SOAP function doesn't need any parameters, an empty array must still be passed to the PHP function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM