我正在尝试每隔一段时间在其中运行一些带有 curl 的 php,以便它从网站检索信息,然后将其插入到我的数据库中。我能够让 cronjob 工作,并且 php 工作,因为我让它每次运行时都会向我发送一封电子邮件,而且确实如此。然而,CURL 脚本和/或 MySQL 脚本不起作用。
定时任务:
* * * * * /usr/bin/php -q /var/www/rstracker/cronjobs.php
cronjobs.php:
<?php
$to = '';
$subject = 'RSTracker Cronjob Ran!';
$message = 'Successful cronjob!';
$headers = 'From: ' . "\r\n" .
'Reply-To: ' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
<?php
include("connect.php");
$query = mysql_query("SELECT id FROM users");
while($row = mysql_fetch_array($query))
{
$userID = $row['id'];
$date = mktime();
// create a new cURL resource
$ch = curl_init();
// set URL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_URL, "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=".$username);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab HTML
$data = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
//echo "<textarea>$data</textarea><br />";
$hs = explode("\n",$data);
$skills = array("Overall","Attack","Defence","Strength","Constitution","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction");
$i = 0;
for($i = 0; $i<count($skills);$i++) {
// Explode each skill into 3 values - rank, level, exp
$stat = explode(',', $hs[$i]);
$out[$skills[$i]] = Array();
$out[$skills[$i]]['rank'] = $stat[0];
$out[$skills[$i]]['level'] = $stat[1];
$out[$skills[$i]]['xp'] = $stat[2];
}
foreach($out as $key => $value)
{
if($value['level']>0||$value['xp']>0||$value['rank']>0)
{
mysql_query("INSERT INTO skills SET userID='$userID', skill='$key', level='".$value["level"]."', xp='".$value["xp"]."', rank='".$value["rank"]."', date='$date'");
}
}
}
?>