Debuggar with fwrite() in PHP

Asked

Viewed 22 times

0

Guys, I came up with the need to consume an API with a very large volume of data, and I need to do this from a language because the API has limitations, and I go from PHP.

The problem is that I need to make several requests and some of them end up giving error 429. When this error happens, I wanted to see it somehow without having to wait for the script to stop running, because sometimes it is more than 10 minutes running and it would be interesting to know what is happening.

To know what was the lost data I arrived at the solution of trying to debuggar half-nut:

$i = 0;
$log_file = fopen('arquivo-log', 'w');

while($i < 5) {
   $req = curl_get('url-qualquer'); // função que eu utilizo, mas nao vem ao caso

   $body = "HTTP Status: {$req['http_code']}, Request Time: $i \n-------------------\n";
   fwrite($log_file, $body);

   sleep(2); // para o script nao rodar muito rapido e eu poder visualizar a interação
   $i++;
}

fclose($log_file);

But the file is written completely when the script is finished running. What I wanted was to see it written every time I got in the loop. I got this by making an Insert in a database, then I see the data being entered throughout the execution, but I do not believe that is the best solution.

Can someone help me? I am doing something wrong or it is impossible to do this since php is interpreted and then compiled completely before some output?

Or is there some other way to do it, maybe with an Xdebug and tals?

1 answer

1


I would try to replace fwrite by file_put_contents, which is basically the same as a fopen, fwrite and fclose at each run.

$i = 0;
$log_file = 'arquivo-log';

while($i < 5) {
   $req = curl_get('url-qualquer'); // função que eu utilizo, mas nao vem ao caso

   $body = "HTTP Status: {$req['http_code']}, Request Time: $i \n-------------------\n";
   file_put_contents($log_file, file_get_contents($log_file) . $body);

   sleep(2); // para o script nao rodar muito rapido e eu poder visualizar a interação
   $i++;
}

If you want the contents of the file to be written in descending chronological order (newest execution above) just invert the concatenation between $body and file_get_contents().

This "technique", speaking of performance, is a little slower than opening the file once, but for DEBUG purposes is more than enough.

EDIT 1:

Instead of using file_get_contents and concatenating with the current content you can pass the FILE_APPEND flag to file_put_contents. So PHP will concatenate the content at the end of the file without emptying it before.

file_put_contents($log_file, $body, FILE_APPEND);

  • 1

    Thank you very much, friend, this is exactly what I needed, and since it is a script for an extreme case, the performance can be put aside, so the answer was perfect :D.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.