douqianzha6213 2018-07-05 18:39
浏览 21

肥皂空信封。 如何处理空响应

I am working for a while now with the SOAP API (self teached during work projects). But one thing always bothers me, is that I can't figure out how to handle an empty response.

For instance: I call the service to get some articles from an ERP system. The input parameter is the article number or the GTIN.

Here is some sample code: https://codeshare.io/5e3EYr

If for instance no GTIN is set (just for understanding) the response is not an array anymore (single or multidimensional). The return would be a soap error like "Fatal error: Cannot use string offset as an array" Because the return is the error message.

I hope you understand my problem. I already tried to check if it is_array and even tried to catch the string. But I always get the "Fatal error: Cannot use string offset..." message.

  • 写回答

2条回答 默认 最新

  • doushajian2018 2018-07-05 18:48
    关注

    Something like ( as an example for my comment )

    function handleShutdown(){
    
       $lasterror = error_get_last();
    
        if (is_null($lasterror)) {
           //normal shutdown 
            return false;
        }
    
        //return error to client as XML, JSON etc.
        // $lasterror['message']
        // $lasterror['type']
        // $lasterror['file']
        // $lasterror['line']
    }
    
    register_shutdown_function('handleShutdown');
    

    I will leave it up to you on how you want to format the error message. The shutdown handler can even catch out of memory errors ... :)

    Obviously, you cant catch anything before it's registered so do it early in execution.

    php.net/manual/en/function.register-shutdown-function.php

    You may also want to look at

    • set_error_handler
    • set_exception_handler

    Then you can have the trifecta of error handling.

    You could use set_error_handler to catch these but you would want to filter out errors of certain verities (in the error handler), an example would be Deprecated or Notice level errors. You can do this with checking the Severity against the error_reporting level you have (bitwise) like this

    if($severity & error_reporting()) 
       //report on these errors.
    

    Notice the single & is a bitwise comparison and differs from the normal AND (&&)

    Now if you want to prevent the error altogether, I would need to see the code (including the line - marked somehow) where it is produced. Otherwise it's just wild guessing.

    In any case when building some kind of service that lacks the normal GUI, it never hurts to have shutdown recovery to send feedback to the client, just make sure to sanitize any output information you share with clients. That way you don't "leak" information that may give away any information that could be used to compromise your application.

    cheers.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化