PHP 中带有标题位置重定向的 Cron 作业

问题描述 投票:0回答:5

我有一个 cron 作业,可以使用 php

header
函数多次加载同一个文件

当脚本从浏览器运行时工作正常,但当使其成为 cron 时我遇到一些问题。

代码如下。我的文件鬃毛是

sample.php

<?php
$id = $_request['id'];
if($id==""){
$id=0;
}
header("Location:sample.php?id=$id");    
?>

但是标题对我不起作用。

我已经改变了我的编码格式

$base = dirname(dirname(__FILE__)); // now $base contains "app"

header("Location:".$base."?id=$id");

但它也不起作用。

我只收到我的 Cron 确认邮件。但邮件包含错误。

我的邮件如下

Status: 302 Moved Temporarily
X-Powered-By: PHP/5.3.21
Set-Cookie: PHPSESSID=ce4d2ee31140477510bfc780c6d0ce48; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location:/home/xxxxxx/public_html/admin/xxxx.php

任何人指导我。我如何设置这种类型的 cron 作业

如何在我的 cron 文件中重定向。因为我要加载大尺寸的数据源文件。所以只有我在问

php header include cron absolute-path
5个回答
2
投票

玉米作为控制台运行而不是作为浏览器运行,您无法发送玉米标头


2
投票

如果您想在 cronjob 中使用 PHP 运行外部脚本,请查看curl。 http://php.net/manual/en/book.curl.php


1
投票

正如其他人所说,你的 cron 没有标头的概念,它只是运行脚本。

如果您希望能够执行类似的操作,请考虑使用 Lynx - 它将允许您调用您的

cron
>
lynx
>
website
。如果您想要 PHP 解决方案,您也可以查看curl。


0
投票

如果您使用 cron 选项卡运行脚本,则无法发送标头。 Crontab 在控制台中运行而不是在浏览器中运行,因此使用 crontab 发送标头没有任何意义。


0
投票

根据任何人的说法,我检查了 PHP 手册并想出了这个函数来做到这一点,顺便说一句,这也是我正在寻找的。

从你的 cron 脚本中调用它:

if ( $getThisUrlAgain = cURLget ( "myCronJobUrl" ) ) {
    // do stuff with $getThisUrlAgain... 
} else {
    // do something if fail
}




function cURLget($url) {
    
    if( !cURLcheckBasicFunctions() ) return false;
    
    $ch = curl_init();
    
    if ($ch) {
        
        ob_start();
        
        if( !curl_setopt($ch, CURLOPT_URL, $url) ) {
            
            curl_close($ch); // to match curl_init()
            return false;
        }
        
        if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return false;
        
        if( !curl_exec($ch) ) return false;
        
        curl_close($ch);
        
        $retval = ob_get_contents();
        ob_end_clean();
        
        return ( $retval ? $retval : true);
        
    }
    
    else return false;
    
}

function cURLcheckBasicFunctions() {
    if( !function_exists("curl_init") &&
        !function_exists("curl_setopt") &&
        !function_exists("curl_exec") &&
        !function_exists("curl_close") ) return false;
    else
        return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.