1

i'm using PHP as a server backend, to create API, and hence it doesn't console log to browser.

However I'm finding it very hard to debug without using console, and I have to use error_log(json_encode($variable)) all the time to write to error log to see what is being returned/received.

Is there anyway I can 'monitor' the API, and use console.log or similar to write to somewhere, where I can view my output live?

Thanks @Chris, for answering my need. So I'm using the following codes to do a simple print to text file and use tail to see the live output. Works brilliantly.

function mylog($data) {
$myFile = "/home/user/html/log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, json_encode($data, JSON_PRETTY_PRINT));
fclose($fh);
}
6
  • Use XDebug for proper debugging. Commented Aug 1, 2018 at 11:02
  • you can use xdebug or log error in file or you can use stackify.com/13-ways-to-tail-a-log-file-on-windows-unix to view errors in error_log live Commented Aug 1, 2018 at 11:04
  • xdebug does it only work on web application? i’m using react native to call php so php is entirely backend. Commented Aug 1, 2018 at 11:07
  • How do You run the PHP backend? Commented Aug 1, 2018 at 11:25
  • Sorry i may not know the right terms to use. My react native application just call the API for e.g. example.com/get/listing/ then it will return something. However during debugging for the API sometimes i want to know the $_GET or $_POST values received.. so I use error_log now Commented Aug 1, 2018 at 11:26

2 Answers 2

4

My advice would be to write to a log file within your PHP:

$myFile = "log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "write this to my file\n");
fclose($fh);

If you have command line access then you can run this command to view the contents of the file live:

tail -f log.txt

This will then show anything written to the file immediately.

Sign up to request clarification or add additional context in comments.

3 Comments

can you advise how do I add a "pretty" var_dump if i were to write to text file? Because there's many Json returns
@SomeoneSpecial You could use print_r($something, true);, this will pretty-print the variable and return it as a string.
thank u, i added fwrite($fh, json_encode($data, JSON_PRETTY_PRINT)); to the code and now it's working beautifully. Not perfect for debugging but enough for me. thanks!!!
-1

This might help.

ini_set('display_errors', 1);

as this will show errors when you call an API. You can check the errors by clicking on the API call made.

1 Comment

This will only show error and warning notifications, but in his question was mentioned a variable dump.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.