我需要在用户操作时执行目录副本,但目录非常大,所以我希望能够执行这样的操作,而无需用户知道副本完成所需的时间.
任何建议将不胜感激.
我有一个PHP脚本需要很长时间(5-30分钟)才能完成.为了防止重要,脚本使用curl从另一台服务器中抓取数据.这就是它花了这么长时间的原因; 它必须等待每个页面加载,然后再处理它并移动到下一页.
我希望能够启动脚本并让它完成,直到它完成,这将在数据库表中设置一个标志.
我需要知道的是如何在脚本运行完成之前结束http请求.另外,php脚本是最好的方法吗?
我知道有很多关于使用CRON运行php文件的帖子.但是,在共享托管的世界中,以及用户的易于设置,我不想让它搞乱.
我在网上发现了另一个与套接字有关的解决方案.只是想让每个人都接受这个,并告诉我这是一个好主意还是坏主意.听起来很有效.
思考?
//Open socket connection to cron.php
$socketcon = fsockopen($_SERVER['HTTP_HOST'],80,$errorno,$errorstr,10);
if($socketcon) {
$socketdata = "GET /cron.php HTTP 1.1\r\nHost: ".$_SERVER['HTTP_HOST']."\r\nConnection: Close\r\n\r\n";
fwrite($socketcon,$socketdata);
//Normally you would get all the data back with fgets and wait until $socketcon reaches feof.
//In this case, we just do this:
fclose($socketcon);
} else {
//something went wrong. Put your error handler here.
}
Run Code Online (Sandbox Code Playgroud)
cron.php:
//This script does all the work.
sleep(200);
//To prove that this works we will create an empty file here, after the …Run Code Online (Sandbox Code Playgroud) 如何在不等待的情况下异步运行PHP代码?我有一个长期运行(几乎无限)应该在服务器启动时运行,并且应该异步处理而无需等待.
我猜可能的选择是:
我在本地服务器上运行PHP脚本,当发生某些事件时会发送电子邮件,例如生日提醒.
请建议如何在不在浏览器中打开页面的情况下实现此目的.
尝试使用html2pdf类自动生成pdfs.我得到了以下代码,工作正常,只有有人必须手动保存PDF.但是,每当添加新产品时,我都希望在没有用户干预的情况下自动将pdf保存到某个文件夹,并将此值存储在数据库中以供将来参考.我如何"静静地"保存pdf,即在后台不显示任何弹出窗口或要求用户进行干预?提前致谢.
include('pdf_content.php');
$content = ob_get_clean();
// convert to PDF
require_once('html2pdf/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
//$html2pdf->Output($file_name.'_'.date("dmY").'.pdf');
$html2pdf->Output($product_id.'_'.$file_name.'_'.date("dmY").'.pdf');
Run Code Online (Sandbox Code Playgroud) 需要使这块代码与其余代码异步。它会收集 wp 帖子并向我的网址发送帖子请求。该插件应异步运行,并且不会妨碍 wordpress 站点的运行。
for ($x=0; $x<=n; $x++) {
$data = posts[$x];
$ch = curl_init('http://myurl.com/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'ACCEPT: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
curl_close($ch);
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以立即发送 HTTP 响应并继续执行脚本。
背景:在我向服务器提出的所有请求中,有一个创建了 Excel 文件(使用 PHPSpreadSheet),因为创建此文件可能需要更长的时间,我正在考虑向客户端响应HTTP 202 状态代码,例如:
header("HTTP/1.1 202 Excel file in process");
并在每次状态代码到达时在主 js 文件上编写一个侦听器,以激活时间间隔 ( setInterval()) 并每隔一定秒数询问 Excel 文件是否准备就绪
$(document).ajaxSuccess(function ( event, xhr, settings) {
if(xhr.status === 202) {
//Activate the setInterval function to ask the server every 10 seconds whether the file is ready or not
}
});
Run Code Online (Sandbox Code Playgroud)
(我知道我必须激活/停用间隔)
因此,每当我收到有关 createExcel.php 文件的请愿书时,我想立即回复用户已收到请愿书并开始制作此类 Excel 文件
就像是:
<?php
//Tell user that the petition has being received
header("HTTP/1.1 202 Excel file in process");
//Kill this …Run Code Online (Sandbox Code Playgroud) 我制作了这个脚本来测试PHP的执行作为后台进程
foreach($tests as $test) {
exec("php test.php ".$test["id"]);
}
Run Code Online (Sandbox Code Playgroud)
正如php过程背景中提出的 以及如何使用PHP通过Google Calendar API添加大量事件通知提醒?和php执行后台进程
但是,如果没有添加test.php,脚本的运行速度就会超过一个脚本.
我究竟做错了什么?
提前致谢!
举例说明: f.php :
d$=$_Get['ad'];
print d$;
Run Code Online (Sandbox Code Playgroud)
索引.php :
for $i=0 to 200000
// run f.php?ad=i$
Run Code Online (Sandbox Code Playgroud)
运行 f.php 但不要等待完成 f.php 怎么办?
我找到了php asyncronus但我现在不知道这是真的有效还是现有的其他解决方案,或者这是最好的解决方案?!
何时使用 exec 以及如何使用?
php ×8
curl ×2
apache ×1
asynchronous ×1
cron ×1
exec ×1
fork ×1
html2pdf ×1
httprequest ×1
javascript ×1
pdf ×1
process ×1
scripting ×1
wordpress ×1