使用PHP从TEXT文件中提取查询字符串,从URL中进行HTTP POST

时间:2014-05-05 17:46:23

标签: php string http post

我想使用PHP查看本地计算机上的文本文件。在文件的第1行,每隔几分钟自动生成一个查询字符串:

Example: ?artist=myartist&title=mytitle&songtype=S&duration=240000

我想每隔5-10秒检查一次文件,然后取出查询字符串,将其附加到

http://localhost:9595

最终的HTTP请求应如下所示:

http://localhost:9595?artist=myartist&title=mytitle&songtype=S&duration=240000

我不是代码编写者,但已经接受了其他人的建议并且接近了(我认为)。

以下代码。

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

3 个答案:

答案 0 :(得分:1)

您的解决方案如下:对PHP进行ajax调用。

以下是every5sec.php文件:

<?php 
$file = fopen("c:\a\nowplaying.txt", 'r');
$line = fgets($file);
fclose($file);
$url = "http://localhost:9595" . $line;
echo $url;
exit;
?>

此处的JavaScript文件:

<script type="text/javascript">

function refresh() {
        $.get('every5sec.php', function(data){
            $.load(data);
        });
}
setTimeout(refresh, 2000); 

</script>

我认为这会奏效。

答案 1 :(得分:0)

感谢您的帮助。

阅读(并阅读了很多内容)并从一些优秀人员的代码帮助后,我能够实现这一目标。我最终使用了下面的代码,它运行良好,但有一个例外。我偶尔会得到一个文件锁,更具体地说是一个错误消息(警告:fopen(nowplaying.txt):无法打开流:第22行的C:\ b \ nowplaying.php中的权限被拒绝) - 它似乎只有在代码尝试获取文件时才会发生。

我可以编写一个异常(如果出现上述错误,脚本将在2秒后重试吗?

<强>更新

我改变了这一部分:

 $file = fopen("nowplaying.txt", 'r');
 $line = fgets($file);
 fclose($file);
 $url = "http://127.0.0.1:9696" . $line; 

要:

$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; 

然后我在批处理文件的末尾添加了一个SLEEP命令,用于启动代码,如果它退出则启动它。到现在为止还挺好。 :)

我的原始代码:

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

}
?>

答案 2 :(得分:-1)

像这样......

$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); 

对于5-10秒的循环,我不会在PHP中这样做,而是尝试将脚本作为计划任务运行。此外,此代码没有错误检查,因此您可能想要添加一些。