I'd like to use PHP to look at a text file on my local machine. On line 1 of the file, a query string is generated automatically every few minutes:
Example: ?artist=myartist&title=mytitle&songtype=S&duration=240000
I'd like to check the file every 5-10 seconds, then take the query string, append it to
http://localhost:9595
Final HTTP request should look like:
http://localhost:9595?artist=myartist&title=mytitle&songtype=S&duration=240000
I'm NOT a code writer but have taken suggestions from others and gotten close (I think).
Code below.
<?php
/**
* This program will check a file every 5 seconds to see if it has changed...if it has, the new metadata will be sent to the shoutcast server(s)
*/
//the path to the file where your song information is placed...it is assumed that everything is on one line and is in the format you wish to send to the server
DEFINE('songfile', "c:\a
owplaying.txt");
//simply copy and paste this for each server you need to add
$serv["host"][] = "127.0.0.1";
$serv["port"][] = "9595";
while(1)
{
$t=time();
clearstatcache();
$mt=@filemtime(songfile);
if ($mt===FALSE || $mt<1)
{
echo "file not found, will retry in 5 seconds";
sleep(5);
continue;
}
if ($mt==$lastmtime)
{
//file unchanged, will retry in 5 seconds
sleep(5);
continue;
}
$da="";
$f=@fopen(songfile, "r");
if ($f!=0)
{
$da=@fread($f, 4096);
fclose($f);
@unlink(songfile);
}
else
{
echo "error opening songfile, will retry in 5";
sleep(5);
continue;
}
$lastmtime=$mt;
for($count=0; $count < count($serv["host"]); $count++)
{
$mysession = curl_init();
curl_setopt($mysession, CURLOPT_URL, "http://".$serv["host"][$count].":".$serv["port"][$count]."/?mode=updinfo&song=".urlencode(trim($da)));
curl_setopt($mysession, CURLOPT_HEADER, false);
curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($mysession, CURLOPT_POST, true);
curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($mysession, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($mysession, CURLOPT_CONNECTTIMEOUT, 2);
curl_exec($mysession);
curl_close($mysession);
}
echo "song updated";
sleep(5);
}
?>
something like this....
$file = fopen("nowplaying.txt", 'r');
$line = fgets($file);
fclose($file);
$url = "http://localhost:9595" . $line;
urlencode(trim($url));
$mysession = curl_init();
curl_setopt($mysession, CURLOPT_URL, $url);
curl_setopt($mysession, CURLOPT_HEADER, false);
curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($mysession, CURLOPT_POST, true);
curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($mysession, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($mysession, CURLOPT_CONNECTTIMEOUT, 2);
curl_exec($mysession);
curl_close($mysession);
As for the 5-10 second loop, I'd stay away from doing that in PHP and instead try running the script as a scheduled task or something. Also, this code has no error checking so you may want to add some.
The solution for yours are as follow make a ajax call to the PHP.
Here is the every5sec.php
file:
<?php
$file = fopen("c:\a
owplaying.txt", 'r');
$line = fgets($file);
fclose($file);
$url = "http://localhost:9595" . $line;
echo $url;
exit;
?>
JavaScript file here:
<script type="text/javascript">
function refresh() {
$.get('every5sec.php', function(data){
$.load(data);
});
}
setTimeout(refresh, 2000);
</script>
I think this will work.
Thank you for all your help.
After reading (and reading a lot) and with some code help from you fine folks I was able to get this working. I ended up using the below code and it's running great with one exception. I get a file lock every once in awhile, more specifically an error message (Warning: fopen(nowplaying.txt): failed to open stream: Permission denied in C:\b owplaying.php on line 22) -- it seems to only happen when the code tries to get the file while it's being updated.
Can I write an exception that (if the above error appears, the script will try again in 2 seconds?
UPDATE
I changed this section:
$file = fopen("nowplaying.txt", 'r');
$line = fgets($file);
fclose($file);
$url = "http://127.0.0.1:9696" . $line;
To:
$filename = 'nowplaying.txt';
$file = fopen($filename, 'r')
or exit("unable to open file ($filename)");
$line = fgets($file);
fclose($file);
$url = "http://127.0.0.1:9696" . $line;
I then put a SLEEP command at the end of the batch file I'm using to launch the code that starts it over if it exits. So far so good. :)
My Original Code:
<?php
while(1)
{
$t=time();
clearstatcache();
$mt=@filemtime("nowplaying.txt");
if ($mt===FALSE || $mt<1)
{
echo "file not found, will retry in 5 seconds";
sleep(5);
continue;
}
if ($mt==$lastmtime)
{
sleep(5);
continue;
}
$file = fopen("nowplaying.txt", 'r');
$line = fgets($file);
fclose($file);
$url = "http://127.0.0.1:9696" . $line;
$lastmtime=$mt;
$options = array(
'http' => array(
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
sleep(5);
}
?>