dtufl26404 2015-11-14 10:36
浏览 40
已采纳

在这种情况下如何调试PHP代码?

I 'm developing an android app which connects to database using PHP API. It's the first time for me to encounter PHP. What happens is that I "POST" parameters to the php code with a URL to connect to it then php makes a query to my database and store them ... the issue is that all I see is what happens in logcat, I have no idea what is goin on with the PHP so if there is something wrong in there what can I do to debug it ?

Note : I 'm already familiar with echos and var dump I 'm looking for fully debugging tool that will allow me to debug the script without actually running it directly and by that I mean accessing it from my android project.

  • 写回答

1条回答 默认 最新

  • doumao1047 2015-11-14 11:28
    关注

    In such cases i log / append all actions on php side into a file - with a simple

    file_put_contents({file}, {data}, FILE_APPEND);

    . u could also catch nearly every error in php with following methods:

    set_error_handler({YOUR_ERROR_HANDLER}, E_ALL);
    
    register_shutdown_function({YOUR_ERROR_HANDLER});
    

    http://php.net/manual/de/function.set-error-handler.php

    http://php.net/manual/de/function.register-shutdown-function.php

    example code of how would i test an envoirement in php, which i could not debug direct:

    <?php
    
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    // will be triggered on every php shutdown - will log the last error (if one occurs)
    register_shutdown_function(function()
    {
        // get last error
        $aError = error_get_last();
    
        if(!empty($aError))
        {
            logAction('called from register_shutdown_function | ' . $aError['message']);
        }
    });
    
    // will be triggered on every error
    set_error_handler(function($iErrCode, $sErrorText, $sErrorFile = '', $sErrorLine = '')
    {
        logAction('called from set_error_handler | Code: ' . $iErrCode . ' Text: ' . $sErrorText . ' File: ' . $sErrorFile . ' Line: ' . $sErrorLine);
    }, E_ALL);
    
    // will be triggered on every exception
    set_exception_handler(function(\Exception $e)
    {
        logAction('called from set_exception_handler | Code: ' . $e->getCode() . ' Text: ' . $e->getMessage() . ' File: ' . $e->getFile() . ' Line: ' . $e->getLine());
    });
    
    // main log / debug method
    function logAction($sText)
    {
        $sDate = date('Y.m.d H:i:s');
        file_put_contents('debug.log', $sDate . ': ' . $sText, FILE_APPEND);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图