gpt4 book ai didi

php - cronjob 无法运行 php 文件

转载 作者:搜寻专家 更新时间:2023-10-31 21:31:18 26 4
gpt4 key购买 nike

我想在特定时间后在 word press 网站上发布相同的数据,但我有一个问题:

cronjob 无法运行 php 文件

命令:

* * * * * /usr/bin/php /var/www/html/news/cronpost.php

我的代码:

require 'config/config.php';
require 'config/autoload.php';
require 'IXR_Library.php';

$post = post::getInstance();
$user_websites = user_websites::getInstance();
$user_websites_posts = user_websites_posts::getInstance();
$spintext = spintext::getInstance();

$result = $user_websites->getuserWebsites();

foreach ($result as $row) {
$minutes = floor((time() - strtotime($row['last_exe_time'])) / 60);
//echo $minutes . "<br>";

if ($minutes >= $row['schedule_time']) {

$result2 = $user_websites_posts->getPost($row['id']);

$id = array();

foreach ($result2 as $row2) {
$id[] = $row2['post_id'];
}

$ids = implode(",", $id);

//echo $ids . "<br>";

$row3 = $post->getRandPostId($row['local_cat_id'], $ids);

//echo $row3['id'] . "<br><br>";

$user_websites->updateuserwebsites($row['id']);

$user_websites_posts->addUserWebsitesPosts($row['id'], $row3['id']);

if ($row['spin_id'] == '1') {

$title = $spintext->api(stripslashes($row3['title']), 'Regular', 'azhar@techpointmedia.com', 'autoblogging', 'true');

$description = $spintext->api(stripslashes($row3['description']), 'Regular', 'azhar@techpointmedia.com', 'autoblogging', 'true');

} elseif ($row['spin_id'] == '2') {

$title = "test title";

$description = "test description";

} elseif ($row['spin_id'] == '3') {

$title = "test title";

$description = "test description";

} elseif ($row['spin_id'] == '4') {

$title = "test title";

$description = "test description";

} elseif ($row['spin_id'] == '0') {

$title = $row3['title'];

$description = $row3['description'];

}

if (isset($row3['image']) && $row3['image'] != "") {

$xmlrpc = $row['url'] . 'xmlrpc.php';
$client = new IXR_Client($xmlrpc);

//$client->debug = true;

$content = array(
'post_status' => $row['draft_post'],
'post_type' => 'post',
'post_title' => $title,
'post_content' => $description,
'terms' => array('category' => array($row['wordpress_cat_id'])),
);
$params = array(0, $row['username'], $row['password'], $content);
$client->query('wp.newPost', $params);
$post_id = $client->getResponse();

$content = array(
'name' => basename($row3['image']),
'type' => $spintext->mime_content_type($row3['image']),
'bits' => new IXR_Base64(file_get_contents($row3['image'])),
true,
);
$client->query('metaWeblog.newMediaObject', 1, $row['username'], $row['password'], $content);
$media = $client->getResponse();
$content = array(
'post_status' => $row['draft_post'],
'mt_keywords' => 'tag1, tag2, tag3',
'wp_post_thumbnail' => $media['id'],
);
$client->query('metaWeblog.editPost', $post_id, $row['username'], $row['password'], $content, true);
} else {
$xmlrpc = $row['url'] . 'xmlrpc.php';
$client = new IXR_Client($xmlrpc);

//$client->debug = true; //optional but useful

$params = array(
'post_type' => 'post',
'post_status' => $row['draft_post'],
'post_title' => $title,
'post_content' => $description,
//'post_excerpt' => 'This is my test excerpt',
'post_author' => $row['author_id'],
'terms' => array('category' => array($row['wordpress_cat_id'])),
);

$res = $client->query('wp.newPost', 1, $row['username'], $row['password'], $params);
}

}

}

如果我用这段代码替换上面的代码,cronjob 会运行它:

class DBConnect {
function Connect() {
$root = "root";
$pass = "12345";
return $db = new PDO('mysql:host=localhost;dbname=testing', $root, $pass);
}
}

class operations {

// for single instance
protected static $instance = null;
protected $pdo = null;

// Constructor : database connection
public function __construct() {

try {
$con = new DBConnect();
$this->pdo = $con->Connect();
//echo "connected";
} catch (PDOException $e) {
echo $e->getMessage();
}
}

// Getting single instance
public static function getInstance() {
if (!isset(static::$instance)) {
static::$instance = new static;
}
return static::$instance;
}

public function insertRecord($tableName, $cols, $values) {

try {
$number_of_rows = 0;

$sql = "INSERT INTO `$tableName`($cols) VALUES($values)";

//echo $sql;

//exit();

$count = $this->pdo->exec($sql);

} catch (PDOException $e) {
echo $e->getMessage();
}

if ($count > 0) {
return true;
} else {
return false;
}
}

public function selectRecords($tableName, $select = '*', $where = '1') {
try {

$sql = "SELECT $select FROM $tableName WHERE $where";
$stmt = $this->pdo->query($sql);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
echo $e->getMessage();
}

return $row;
}

public function updateRecord($tableName, $fields, $where) {

try {
$number_of_rows = 0;

$sql = "UPDATE `$tableName` SET $fields WHERE $where";

//echo $sql;
//exit();

$count = $this->pdo->exec($sql);

} catch (PDOException $e) {
echo $e->getMessage();
}

if ($count > 0) {
return true;
} else {
return false;
}
}

}

$show = operations::getInstance();

$result = $show->selectRecords("test2");

//$post->insertRecord("test", "message", "'new message'");
//echo "abc";

foreach ($result as $row) {
echo $row['message'] . "<br>";
$minutes = round(abs(time() - strtotime($row['last_exe_time'])) / 60, 0);
echo $minutes . " min<br><br>";

if ($minutes >= $row['tm'] && $minutes > 0) {

$show->insertRecord("test", "message", "'$row[message]'");

$show->updateRecord("test2", "last_exe_time=CURTIME()", "id=$row[id]");

}
}

请告诉我为什么 cronjob 无法运行我的第一个代码。

最佳答案

首先,你尝试运行/usr/bin/php/var/www/html/news/cronpost.php在控制台中,如果它运行成功,你可以尝试使用 shell 脚本。创建一个 shell 脚本文件。示例 /root/cron.sh 内容为 /usr/bin/php/var/www/html/news/cronpost.php然后对您的脚本使用 chmod +x。最后你创建了一个这样的 cronjob

* * * * */root/cron.sh

关于php - cronjob 无法运行 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29767522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com