0

I want to synchronize my app data ( stock ) with that of live ones . I am using Yahoo API .. Here is my code ..

       // $companyInfo ; holds company information like symbol 

        foreach($companyInfo as $singleCompany)
        {
            $feedUrl = 'http://finance.yahoo.com/d/quotes.csv?s='.$singleCompany['Company']['Symbol'].'&f=l1c6gho&e=.csv';

            $handle = fopen($feedUrl, "r");

            $liveCompanyData = fgetcsv($handle);  
            if(!empty($liveCompanyData) && isset($liveCompanyData))
            { 
                      /***  here I parse data n save it in db ***/
            }
           fclose($handle);

        }

Above code will work for small set of data (i.e for records 30 approx ), and for long set of records it will prompt me Maximum execution time of 60 seconds exceeded in ..... how can I do that ?

NOTE : I am using cakephp framework.

lxa
  • 3,234
  • 2
  • 30
  • 31
anasanjaria
  • 1,308
  • 1
  • 15
  • 19

1 Answers1

0

You may use set_time_limit function:

if(!ini_get('safe_mode') ) { 
    set_time_limit(120); 
} 

However, it will not work if PHP is in safe mode (see that if). In that case, you've got to either turn off safe mode, or limit script execution time - for example by processing only one company at a time, and executing your script separately for each company.

And since you say you're using CakePHP, I assume that your code runs in user-requested script - which is not the best design, as it is actually a service script. It would be better to run it as a cron job (php -f your_script.php) - in this case, different php configuration file is used (/etc/php5/cli/php.ini usually), and if you have access/privileges to edit it - change max_execution_time parameter in it (it will affect only php-cli scripts).

lxa
  • 3,234
  • 2
  • 30
  • 31
  • When user opens a page (script URL) in his browser. As opposed to cron job - when script URL is requested by cron scheduler. – lxa Jul 19 '11 at 18:59
  • can u plz tell me what do u mean by user-requested script ? What I understand is ... since i have written code within controller so it will utilize all mvc resources and all that , on the other hand if I write simple php script then it will be light weight server script and takes less time and resources as that of mvc one.. M i correct ? – anasanjaria Jul 19 '11 at 19:03
  • I have created a normal php script and will hit yahoo api for data as many time as my records are ... so is it a good approach ? – anasanjaria Jul 19 '11 at 19:10
  • That doesn't actually matter. What matters is "run time" of your script - i.e. how long it runs from start to end. To reduce this time - split it into several runs. I.e. if your `$companyInfo` contains, say, 1000 symbols - do 100 in one run, and in next run start from the point where previous left. You can save position where you left off in DB or in file. – lxa Jul 19 '11 at 20:51
  • I have created a crone-job & place it under webroot folder. Now I want to configure it on live server. So should I do it like this ? Go to your domain cpanel account. First make the file which you want to run the code using cron job. and upload the file on your website root folder. Now Go to "Cron job" section in your cpanel account and enter the file path in textbox and set the time shedule which you want to run the file. is it ok ? I have found [this link too](http://www.utoxin.name/2009/07/cakephp-shells-and-cron-jobs/) . Can u guide me ? – anasanjaria Aug 20 '11 at 12:09