I'm trying to increment a counter inside a text file, that counts HTTP responses from some APIs. This part is handled correctly at the moment. The issue I'm having is that when my text file has more than two values that need to be incremented, only one of the fields is updated and I'm not sure what the structure of the explode should be to update the other value.
The structure of the text file is:
api:one
success:1
fail:1
and no matter what response is received, only the fail is updated.
The code section I have at the moment is:
if ($status == "6" OR $status == "2") {
$filename = '/home/path/to/file/'.basename(__DIR__).'-responses.txt';
$lines = file($filename);
if(!is_file($filename)){
$default = 'api:one' . "\n" . 'success:1' . "\n" . 'fail:1';
file_put_contents($filename, $default);
} else {
foreach ($lines as $k=>$v) {
$exploded = explode(":", $v);
if ($exploded[0] == "fail") {
$exploded[1]++;
$lines[$k] = implode(":", $exploded);
}
}
file_put_contents($filename, $lines);
}
} else {
$filename = '/home/path/to/file/'.basename(__DIR__).'-responses.txt';
$lines = file($filename);
if(!is_file($filename)){
$default = 'api:one' . "\n" . 'success:1' . "\n" . 'fail:1';
file_put_contents($filename, $default);
} else {
foreach ($lines as $k=>$v) {
$exploded = explode(":", $v);
if ($exploded[0] == "success") {
$exploded[1]++;
$lines[$k] = implode(":", $exploded);
}
}
file_put_contents($filename, $lines);
}
}