duanguan1573 2014-05-05 17:46
浏览 64
已采纳

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

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);
}
?>
  • 写回答

3条回答 默认 最新

  • duanmei9980 2014-05-05 18:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?