Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP script overloads the server
    text
    copied!<p>I have to run a php script daily, the code is this: </p> <pre><code>&lt;?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 &amp;&amp; !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 &gt;= 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-&gt;getUser(); // Login or logout url will be needed depending on current user state. if ($user) { $logoutUrl = $facebook-&gt;getLogoutUrl(); } else { $loginUrl = $facebook-&gt;getLoginUrl( array( 'scope' =&gt; '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-&gt;api(array( 'method' =&gt; 'fql.query', 'query' =&gt; $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-&gt;getLoginUrl(); echo 'Please &lt;a href="' . $login_url . '"&gt;login.&lt;/a&gt;'; error_log($e-&gt;getType()); error_log($e-&gt;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(); ?&gt; </code></pre> <p>Basically it is a cycle that for every picture in my website takes and FQL query from Facebook and store the result in my MYSQL database. I even makes a log of the files done.</p> <p>Problems: - if i call it from the browser it locks up the server and any php file i try to load waits forever and gives a 500 server error. - i don't want it to timeout because i want it to cycle all the pictures in the database and refresh them with the new values</p> <p>Resolutions - Cron Job (this should allow the script to run in "background" mode right? - Split the script in different parts processing 10 pictures per time (not good) - Use proc_nice() before the code to assign a lower priority to the whole php instructions.</p> <p>Actually i have this script overloading the server and anything gets unusable, what do you think about that ?</p> <p>Thanks a lot !!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload