Search code examples
phpcakephpstockquotesyahoo-api

Sync. stock data with that of live ones


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.


Solution

  • 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).