我必须每天运行PHP脚本,代码是这样的:PHP脚本重载服务器
<?php
// SET PROCESS PRIORITY
// SETTING UP THE LOG FILE
$ob_file = fopen('sync.log','w');
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
}
ob_start('ob_file_callback');
// GET PARAMETERS
$lastid=$_GET['lastid'];
if ($lastid && !is_numeric($lastid)){
die("Loser");
}
// SERVER ROOT CONFIGURATION
$serverpath = 'http://dev.danielepiccone.com/ops/';
// FACEBOOK SDK
require '../classes/facebook.php';
require '../classes/fbconfig.php';
// MYSQL CONFIG
include('../dbconfig.php');
// OPEN DATABASE CONNECTION $CON
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname, $con);
// GET LAST ID FROM DATABASE AND COUNT TO ZERO
// ONLY 10 PER TIMES
$sql = "SELECT * FROM ops_pictures
ORDER BY id DESC
LIMIT 10;";
$query = mysql_query($sql,$con);
while($row = mysql_fetch_array($query)) {
if (!$lastid){
$lastid = $row['id'];
}
}
//START COUNTING
for ($i = $lastid; $i >= 0 ; $i --)
{
// set_time_limit(); // RESET TIMEOUT TO GO FOREVER
echo $i .' - ';
// LOAD RECORDS FROM FACEBOOK AND DISPLAY THE PHOTO URL
// Get User ID
$user = $facebook->getUser();
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,user_likes,read_stream,publish_stream,user_about_me,user_photos'
)
);
};
// FQL QUERY FOR THE PICTURE
$actualurl = $serverpath . 'photo.php?pid=' . $i;
//echo $actualurl;
try {
$fql = 'SELECT url, normalized_url, share_count, like_count, comment_count, total_count,
commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url="'.$actualurl.'"';
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
// FQL queries return the results in an array, so we have
// to get the user's name from the first element in the array.
$linkstat = $ret_obj[0];
$theurl = $linkstat['url'];
$sharecount = $linkstat['share_count'];
$likecount = $linkstat['like_count'];
$commentcount = $linkstat['comment_count'];
$totalcount = $linkstat['total_count'];
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
// PUT THE RECORDS IN
$sql = "UPDATE ops_pictures SET likecount='$likecount', sharecount='$sharecount', totalcount ='$totalcount'
WHERE id='$i'";
mysql_query($sql,$con);
//END COUNT
}
// CLOSE DATABASE
mysql_close($con);
ob_end_flush();
?>
基本上它是一个周期,对于在我的网站的每个画面拍照并从Facebook FQL查询和存储导致我的MYSQL数据库。我甚至会记录完成的文件。
问题: - 如果我从浏览器调用它锁定服务器和任何php文件,我尝试加载等待永远,并给出500服务器错误。 - 我不希望它超时,因为我希望它循环数据库中的所有图片,并用新值刷新它们
决议 - cron作业运行(这应该允许脚本在“后台”运行模式右 - 斯普利特的不同零件加工每单位时间(不好)10张图片 脚本 - 使用proc_nice()的代码之前低优先级分配给整个PHP指令
其实我有这个脚本超载服务器和任何东西无法使用,你怎么看?
谢谢很多!
什么'回声$ lastid'打印? – 2012-07-13 19:07:15
在数据库中记录文件不容易吗? – 2012-07-13 19:10:20
服务器可能会做很多IO,运行'iotop'来确认。另一个可能是更新语法,如果没有事务处理,这是很痛苦的。 – Pentium10 2012-07-13 19:10:31