2

对 PHP 手册的评论指出:

如果您使用此方法,请记住需要传入参数数组,其顺序与 SOAP 端点期望的顺序相同。

例如 //server 期望:Foo(string name, int age)

//won't work
$args = array(32, 'john');
$out = $client->__soapCall('Foo', $args);

//will work
$args = array('john', 32);
$out = $client->__soapCall('Foo', $args);

我正在构建一个动态分配参数值的 SOAP 客户端,这意味着参数并不总是按正确的顺序排列。然后这会中断实际的 SOAP 调用。

除了检查每个调用的参数顺序之外,是否有一个简单的解决方案?

4

3 回答 3

3

我在动态添加 SOAP 参数时遇到了同样的问题,我必须以正确的顺序获取它们才能使我的 SOAP 调用正常工作。

因此,我必须编写一些东西来从 WSDL 中获取所有 SOAP 方法,然后确定以何种顺序排列方法参数。

幸运的是,PHP 使用 '$client->__getFunctions()' 方法可以很容易地获取 SOAP 函数,因此您需要做的就是搜索要调用的服务方法,该方法将包含正确顺序的方法参数和然后进行一些数组匹配以以相同的顺序获取您的请求参数数组。

这是代码...

    <?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());
    }
于 2011-06-23T16:22:37.447 回答
1

命名参数存在一个简单的解决方案:

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);

它不仅对参数进行正确排序,还过滤数组中的参数。

于 2010-12-23T07:12:54.987 回答
0

另一种解决方案是验证 wsdl 中的 xsd 文件。PHP SOAP 根据 xsd 文件中的参数顺序构造请求。

于 2019-09-14T23:54:32.417 回答