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

时间:2015-11-14 10:36:22

标签: php android

我正在开发一个使用PHP API连接到数据库的Android应用程序。这是我第一次遇到PHP。 会发生什么是我" POST" php代码的参数用一个URL连接到它然后php对我的数据库进行查询并存储它们...问题是我所看到的只是在logcat中发生了什么,我不知道什么是用PHP进行的所以如果有什么问题我可以做些什么来调试呢?

注意:我已经熟悉echos和var dump我正在寻找完全调试工具,这将允许我调试脚本而不直接运行它,我的意思是访问它我的android项目。

1 个答案:

答案 0 :(得分:0)

在这种情况下,我将php侧的所有操作记录/附加到文件中 - 使用简单的

  

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

。 你还可以通过以下方法捕获php中的几乎所有错误:

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

如何在php中测试envoirement的示例代码,我无法直接调试:

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