可能的选项包括:
1. 在网页中运行代码并保持打开状态以执行该任务。 2. 从某个命令行实用工具(我不确定如何)调用脚本,在后台进行处理。
我在本地服务器上运行PHP脚本,当某些事件发生时,例如生日提醒,将发送电子邮件。
请建议我如何在不打开浏览器的情况下实现这一点。
<?php
$params['my_param'] = $a_value;
post_async('http:://localhost/batch/myjob.php', $params);
/*
* Executes a PHP page asynchronously so the current page does not have to wait for it to finish running.
*
*/
function post_async($url, array $params)
{
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
$out = "POST ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
if (isset($post_string)) $out.= $post_string;
fwrite($fp, $out);
fclose($fp);
}
假设上述文件位于你的Web根目录(例如/var/www
)中,名为runjobs.php
。通过访问http://localhost/runjobs.php
,你的myjob.php
文件将开始运行。你可能想要在浏览器中添加一些输出以让你知道它已经成功提交,并且如果你的Web服务器对外开放,则最好添加一些安全性。这个解决方案的一个不错的特点是,如果你添加了一些安全性,你可以在任何你能找到浏览器的地方启动作业。
$out = "POST ".$parts['path']." HTTP/1.1\r\n";
替换为以下内容:if (!empty($parts['query'])) {
$query = "?".$parts['query'];
}
$out = "GET ".$parts['path']." HTTP/1.1\r\n";
- allofmex这绝对是一个cron任务。您可以设置一个php脚本,执行一次您的任务,并且可以根据需要定期运行cron。 这里有一篇很好的文章,介绍如何将php脚本设置为cron任务;非常容易实现。
这并不是 PHP 的设计初衷。您需要使用 PECL 线程库来启动异步运行的线程,但我不建议这样做。在异步部分的新热点是 node.js - 我建议您研究一下并看看是否可以利用它。它专为轻量级、异步网络操作而设计,并可用于触发 PHP 脚本。
root# php yourscript.php &
其中yourscript.php包含类似于以下内容
<?php
$log = fopen('/var/log/yourscript.log', 'a+');
// ### check if we are running already omitted
while(true) {
// do interesting stuff and log it.
// don't be a CPU hog
sleep(1);
}
?>
装饰:
为了使您的脚本可以直接执行:chmod +x yourscript.php,并在yourscript的开头添加#!/usr/bin/php
要开始使用apache,您应该将该命令添加到您的apache启动脚本(通常是apachectl),并确保在apache停止时添加代码来终止它。
检查是否已经运行涉及到一个带有/var/locks/中PID文件的内容,以及类似system('/bin/ps '.$thePID)的东西。这也使得kill指令更容易编写。
感谢Todd Chaffee,但这对我没有用,所以我编辑了你的代码,希望你不介意,也许它还能帮助其他人使用这种技术。
cornjobpage.php //主页面
<?php
//if you want to call page for multiples time w.r.t array
//then uncomment loop start & end)
?>
<?php
//foreach ($inputkeywordsArr as $singleKeyword) {
$url="http://localhost/projectname/testpage.php";
$params['Keywordname'] = "testValue";//$singleKeyword
post_async($url, $params);
//}//foreach ($inputkeywordsArr end
?>
<?php
/*
* Executes a PHP page asynchronously so the current page does not have to wait for it to finish running.
*
*/
function post_async($url, array $params)
{
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
$out = "GET ".$parts['path']."?$post_string"." HTTP/1.1\r\n";//you can use POST instead of GET if you like
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fclose($fp);
}
?>
testpage.php
<?
echo $_REQUEST["Keywordname"];//Output > testValue
?>