2010-11-19 75 views
2

在PHP手冊指出A comment爲什麼SOAP參數的順序很重要,以及如何解決它?

如果您在使用此方法時,請記住 是的參數數組必須 與排序作爲 順序相同SOAP端點 希望通過英寸

如 //服務器期望:美孚(字符串名稱,詮釋年齡)

//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()); 
    } 
相關問題