2

PHPマニュアルへのコメントは次のように述べています。

このメソッドを使用している場合は、引数の配列を、SOAPエンドポイントが期待するのと同じ順序で渡す必要があることに注意してください。

例://サーバーが期待するもの: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 に答える