是否可以从PHP脚本与HTML页面位于同一服务器上的PHP脚本中为某些HTML页面发出HTTP请求?服务器启动时,此脚本必须始终运行。
PHP脚本必须每隔一秒发出一次请求。
我尝试过一项cron工作,但准确性还不够。
我在javascript例程中使用了nodejs,这对我有用,但我宁愿让服务器端脚本来处理请求。
我想使用异步方法来发出请求,因为HTTP请求不会干扰时间。
我不想让计算机始终只是在该页面上。所以我需要使用托管站点的服务器的其他解决方案。
答案 0 :(得分:0)
下面是一个经过测试的异步PHP请求
它可以从同一台服务器或任何服务器运行
准确度应在毫秒之内
减少usleep(1000)将提高准确性但降低CPU可用性。
在投票之前我的回答请确保您了解我的所作所为
在我的测试中,我监控了CPU的使用情况。典型用法为0.1%CPU和最大0.2%
在我的下面的脚本中,这是HTTP请求标头:
$http = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
可以对其进行编辑,以在标题中包含您需要的任何内容 添加用户代理:
$ua = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:48.0) Gecko/20100101 Firefox/48.0'
然后添加crlf和$ ua
$http = "GET $path HTTP/1.0\r\nHost: $host\r\n$ua\r\n\r\n";
这个PHP脚本是http://ispeedlink.com/so/asyncTimer/index.php
它向http://ispeedlink.com/so/asyncTimer/log.php""发出HTTP请求每一秒
<?php
$host = 'ispeedlink.com';
$path = '/so/asyncTimer/log.php';
$http = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
$cntr = 0;
while(true){
if(((microtime(true) * 1000) % 10) < 1){ // make http request as soon as system clock passes one second + 10mS
$stream = stream_socket_client("$host:80", $errno,$errstr, 100,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
if ($stream) {
fwrite($stream, $http);
}
else {
file_put_contents('error.txt',"Failed: $errno,$errstr\n",FILE_APPEND);
}
usleep(990000); // wait 990 milliseconds so microtime() * 1000 % 10 will be greater than 10 until next second
$cntr++;
if($cntr > 9){break;}
}
usleep(1000); // release CPU for at least a millisecond.
}
header('Content-Type: text/plain; charset=utf-8');
readfile('log.txt');
file_put_contents('log.txt',"\n",FILE_APPEND);
?>
对于这个测试,我添加了一个计数器,允许脚本只发出10个请求:
$cntr++;
if($cntr > 9){break;}
这是每秒从index.php请求的log.php。
<?php
$time = microtime(true) . "\n";
file_put_contents('log.txt',$time,FILE_APPEND);
?>
在我的浏览器中,我向http://ispeedlink.com/so/asyncTimer/
发出了请求
两次之间暂停。
这是log.txt
1462802817.32
1462802818.32
1462802819.32
1462802820.32
1462802821.32
1462802822.32
1462802823.32
1462802824.32
1462802825.32
1462802826.33
1462802836.72
1462802837.71
1462802838.72
1462802839.72
1462802840.72
1462802841.72
1462802842.72
1462802843.72
1462802844.72
1462802845.71
如果您需要查看请求的回复 在请求后添加以下代码:
$sockets[] = $stream;
这将是这样的:
if ($stream) {
fwrite($stream, $http);
$sockets[] = $stream;
}
并添加此例程
每个请求的响应都在$ response数组中。
while (count($sockets)) {
$read = $sockets;
stream_select($read, $write = NULL, $except = NULL, 100);
if (count($read)) {
foreach ($read as $r) {
$id = array_search($r, $sockets);
$data = fread($r, $buffer_size);
if (strlen($data) == 0) {
fclose($r);
unset($sockets[$id]);
}
else {
$response[$id] .= $data;
}
}
}
else {
break;
}
}
CPU使用率统计信息:
%CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
0.2 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0.1 0.0 233524 11236 ? S 11:10 0:00 /usr/bin/php /home/usr/public_html/so/asyncTimer/index.php
0