A comment on the PHP manual states:
If you are using this method, remember that the array of arguments need to be passed in with the ordering being the same order that the SOAP endpoint expects.
e.g //server expects: Foo(string name, int age)
//won't work $args = array(32, 'john'); $out = $client->__soapCall('Foo', $args); //will work $args = a开发者_如何学运维rray('john', 32); $out = $client->__soapCall('Foo', $args);
I'm building a SOAP client that dynamically assigns the argument values, which means that it happens that the arguments aren't always in the correct order. This then breaks the actual SOAP call.
Is there an easy solution to this, short of checking the order of the parameters for each call?
I had the same problem where I dynamically added the SOAP parameters and I had to get them in the correct order for my SOAP call to work.
So I had to write something that will get all the SOAP methods from the WSDL and then determine in which order to arrange the method arguments.
Luckily PHP makes it easy to get the SOAP functions using the '$client->__getFunctions()' method, so all you need to do is search for the service method you want to call which will contain the method arguments in the correct order and then do some array matching to get your request parameter array in the same order.
Here is the code...
<?php
// Instantiate the soap client
$client = new SoapClient("http://localhost/magento/api/v2_soap?wsdl", array('trace'=>1));
$wsdlFunctions = $client->__getFunctions();
$wsdlFunction = '';
$requestParams = NULL;
$serviceMethod = 'catalogProductInfo';
$params = array('product'=>'ch124-555U', 'sessionId'=>'eeb7e00da7c413ceae069485e319daf5', 'somethingElse'=>'xxx');
// Search for the service method in the wsdl functions
foreach ($wsdlFunctions as $func) {
if (stripos($func, "{$serviceMethod}(") !== FALSE) {
$wsdlFunction = $func;
break;
}
}
// Now we need to get the order in which the params should be called
foreach ($params as $k=>$v) {
$match = strpos($wsdlFunction, "\${$k}");
if ($match !== FALSE) {
$requestParams[$k] = $match;
}
}
// Sort the array so that our requestParams are in the correct order
if (is_array($requestParams)) {
asort($requestParams);
} else {
// Throw an error, the service method or param names was not found.
die('The requested service method or parameter names was not found on the web-service. Please check the method name and parameters.');
}
// The $requestParams array now contains the parameter names in the correct order, we just need to add the values now.
foreach ($requestParams as $k=>$paramName) {
$requestParams[$k] = $params[$k];
}
try {
$test = $client->__soapCall($serviceMethod, $requestParams);
print_r($test);
} catch (SoapFault $e) {
print_r('Error: ' . $e->getMessage());
}
An easy solution exists for named parameters:
function checkParams($call, $parameters) {
$param_template = array(
'Foo' => array('name', 'age'),
'Bar' => array('email', 'opt_out'),
);
//If there's no template, just return the parameters as is
if (!array_key_exists($call, $param_template)) {
return $parameters;
}
//Get the Template
$template = $param_template[$call];
//Use the parameter names as keys
$template = array_combine($template, range(1, count($template)));
//Use array_intersect_key to filter the elements
return array_intersect_key($parameters, $template);
}
$parameters = checkParams('Foo', array(
'age' => 32,
'name' => 'john',
'something' => 'else'
));
//$parameters is now array('name' => 'john', 'age' => 32)
$out = $client->__soapCall('Foo', $parameters);
Not only does it correctly order the parameters, it also filters the parameters in the array.
Another solution is to verify the xsd files from your wsdl. PHP SOAP construct the request based on parameters order in xsd files.
精彩评论