首页 > 解决方案 > 如何在 PHP 中使用 Cronjob 触发 URL?

问题描述

我的主机不支持 CronJob,因此我正在尝试使用 WordPress 内置事件函数wp_schedule_event运行 Cron ,代码似乎正在运行,但是,我正在修改我的代码。我想点击一个 URL 并将响应存储在一个文本文件中,以确保 HIT 已经完成,但我似乎无法弄清楚如何点击一个 URL?

这是代码

// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
    wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );

现在在这个函数中,注释代码添加了一个新的 WordPress 帖子,告诉我代码正在运行。但是,当我添加一个 URL 时它没有命中。

function every_three_minutes_event_func() {
// I tried this way but no luck 
header('Location: http://example.com');


/* Gather post data.
$my_post = array(
    'post_title'    => 'My post',
    'post_content'  => 'This is my post.',
    'post_status'   => 'publish',
);
 
// Insert the post into the database.
wp_insert_post( $my_post ); */

}

我必须为此使用 cURL 吗?

请任何指导将不胜感激。非常感谢。

标签: phpwordpresscurlcron

解决方案


推荐阅读