2010-11-19 20 views
2

A commentPHP SOAPでSOAPパラメータの順序が重要なのはなぜですか? PHPマニュアルの状態に

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

例えば //サーバが期待し:フー(文字列名、int型の年齢)

//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呼び出しが中断されます。

各コールのパラメータの順番を確認するのは簡単ですか?

答えて

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

だけでなく、それは正しくパラメータを注文ん、それはまた、アレイ内のパラメータをフィルタリングします。

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()); 
    } 
関連する問題