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);
}
?>