我必须每天运行一个 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.xxx.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 指令。
实际上我有这个脚本使服务器超载并且任何东西都无法使用,你怎么看?
非常感谢 !!