在PHP中调试SOAP调用:由于客户端数据而引发了异常

时间:2013-04-04 00:14:34

标签: php api soap wsdl

我正在使用UPS api,我很难调试。我得到以下堆栈跟踪:

Details
Type: SoapFault
Message: An exception has been raised as a result of client data.
File: /Users/shawn/Documents/work/sites/Wingspan/kaleco/lib/SixString/Utilities/Ups.php
Line: 161
Trace

#0 /Users/shawn/Documents/work/sites/Wingspan/kaleco/lib/SixString/Utilities/Ups.php(161): SoapClient->__soapCall('ProcessShipment', Array)
#1 /Users/shawn/Documents/work/sites/Wingspan/kaleco/routes/ups.php(14): SixString\Utilities\Ups->getShipment()
#2 [internal function]: {closure}()
#3 /Users/shawn/Documents/work/sites/Wingspan/kaleco/vendor/slim/slim/Slim/Router.php(172): call_user_func_array(Object(Closure), Array)
#4 /Users/shawn/Documents/work/sites/Wingspan/kaleco/vendor/slim/slim/Slim/Slim.php(1222): Slim\Router->dispatch(Object(Slim\Route))
#5 /Users/shawn/Documents/work/sites/Wingspan/kaleco/vendor/slim/slim/Slim/Middleware/Flash.php(86): Slim\Slim->call()
#6 /Users/shawn/Documents/work/sites/Wingspan/kaleco/vendor/slim/slim/Slim/Middleware/MethodOverride.php(94): Slim\Middleware\Flash->call()
#7 /Users/shawn/Documents/work/sites/Wingspan/kaleco/vendor/slim/slim/Slim/Middleware/SessionCookie.php(116): Slim\Middleware\MethodOverride->call()
#8 /Users/shawn/Documents/work/sites/Wingspan/kaleco/vendor/slim/slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\SessionCookie->call()
#9 /Users/shawn/Documents/work/sites/Wingspan/kaleco/vendor/slim/slim/Slim/Slim.php(1174): Slim\Middleware\PrettyExceptions->call()
#10 /Users/shawn/Documents/work/sites/Wingspan/kaleco/public/index.php(6): Slim\Slim->run()
#11 {main}

以下是代码块:

    try{
        $mode = array('soap_version' => 'SOAP_1_1',  'trace' => 1,'exceptions' => true );
        $client = new \SoapClient($wsdl , $mode);
        $client->__setLocation($endpointurl);

        $header = new \SoapHeader('http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','UPSSecurity',$this->upss);
        $client->__setSoapHeaders($header);

        if(strcmp($operation,"ProcessShipment") == 0 ) {
            $resp = $client->__soapCall('ProcessShipment',array($this->processShipment()));
            echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";
            //print_r($client->__getLastRequest());
        }
        else if (strcmp($operation , "ProcessShipConfirm") == 0) {
            $resp = $client->__soapCall('ProcessShipConfirm',array($this->processShipConfirm()));
            echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";
        }
        else {
            $resp = $client->__soapCall('ProcessShipeAccept',array($this->processShipAccept()));
            //get status
            echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";
            echo "<pre>";
            print_r($resp);
            echo "</pre>";
        }
    }
    catch(Exception $ex){
        print_r ($ex);
    }

第161行在上面的代码中,这是实际的行:

$resp = $client->__soapCall('ProcessShipConfirm',array($this->processShipConfirm()));

我正在寻找一种调试方法。它似乎没有到达catch块,我似乎无法确定异常的原因。

4 个答案:

答案 0 :(得分:3)

尝试使用SoapFault捕获异常并打印其detail属性。

try {

   $response = $client->$call( $params );

} catch (\SoapFault $fault) {

    //trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
    var_dump($fault->faultcode);
    var_dump($fault->faultstring);
    var_dump($fault->detail);
    die('Errore chiamata webservice UPS');

}

请参阅:http://php.net/manual/en/function.is-soap-fault.php

答案 1 :(得分:1)

我不知道PHP中异常处理的细微差别,但是Soap异常在C#中也是一个小问题。在Visual Studio的调试器中,您可以直接在IDE中获取未处理的异常的详细信息。如果您有选项可以执行此操作,请查找异常的"detail" XML元素,因为这将包含Web服务的消息,告诉您对您的请求有何不满。

至于实际捕获异常,@ Jeff Atwood写的一篇陈旧但好的文章 - 尽管为(喘息)VB.NET编写 - 可能仍然提供了一些有关Soap异常的见解,呃...滑... Coding Horror | Throwing better SOAP exceptions

答案 2 :(得分:1)

注意:对于它的价值, UPS会为错误的跟踪号码提供此错误。使用良好(有效)的跟踪号码,它纠正了我的问题。

答案 3 :(得分:1)

UPS也为Rate API中的错误ShipFrom / CountryCode或ShipTo / CountryCode提供此错误。例如德国无法评级。这不能在UPS开发文档中找到。