Skip to content

Instantly share code, notes, and snippets.

@andyg2
Created March 15, 2022 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyg2/1aa56a096ed6166b239af46ce33cd47c to your computer and use it in GitHub Desktop.
Save andyg2/1aa56a096ed6166b239af46ce33cd47c to your computer and use it in GitHub Desktop.
Write Stream to File
<?php
$ip = '192.168.2.113';
// 192.168.2.114
// 192.168.2.120
$path = './'.$ip.'-stream-'.date('Y-m-d-H-i-s').'.xml';
$url = 'http://'.$ip.'/clickit/analyticsdata.cgi?timeout=100000';
do_capture($url, $path);
function do_capture($url, $path){
touch($path);
if ($stream = fopen($url, 'r')) {
// Open save file at end of file
if($fh = fopen($path, 'a')){
$contents = stream_get_contents($stream);
if($contents){
if(fwrite($fh, $contents)){
// OK
}else{
// cannot write contents
touch($path);
if(!file_exists($path)){
exit(10); // not creatable- HD full?
}else{
if(!is_writable($path)){
exit(9); // not writable - HD full?
}
}
}
}else{
// OK, no contents
}
}else{
// cannot open save file for writing
sleep(10);
exit;
}
}else{
// cannot open stream for reading
sleep(10);
exit;
}
// return(do_capture($url, $path));
}
// no matter what, just reload.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment