我必須每天運行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