Jump to content

I get this error occasionaly


kingb00zer
 Share

Recommended Posts

Hi on occasion I get the error pasted below and am wondering if there is a way for it to either not be displayed or for it to not give up after 30 seconds? thanks

 

Fatal error: Maximum execution time of 30 seconds exceeded in

Link to comment
Share on other sites

The default max execution time for PHP scripts is 30 seconds. If ran longer than that, you get an error like the one you saw. You can increase the execution time in your php.ini file by increasing the max_execution_time directive.

Link to comment
Share on other sites

Show us your code. Something in your code is taking far too long to run, it could be an infinite loop or a slow MySQL query.

the code is pasted below, I created something based around some code I found form a google search which gives me stock prices from yahoo finance. its only ever shows me this error 50% of the time while the market is closed. its no big deal if I can extend the time I just need to know how to change that time.

 

<?php
/**
* Class to fetch stock data from Yahoo! Finance
*
*/
class YahooStock {
/**
* Array of stock code
*/
private $stocks = array();
/**
* Parameters string to be fetched
*/
private $format;
/**
* Populate stock array with stock code
*
* @param string $stock Stock code of company
* @return void
*/
public function addStock($stock)
{
$this->stocks[] = $stock;
}
/**
* Populate parameters/format to be fetched
*
* @param string $param Parameters/Format to be fetched
* @return void
*/
public function addFormat($format)
{
$this->format = $format;
}
/**
* Get Stock Data
*
* @return array
*/
public function getQuotes()
{
$result = array();
$format = $this->format;
foreach ($this->stocks as $stock)
{
/**
* fetch data from Yahoo!
* s = stock code
* f = format
* e = filetype
*/
$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=$format&e=.csv"); // this is the line that causes the fatal error (sometimes)
/**
* convert the comma separated data into array
*/
$data = explode( ',', $s);
/**
* populate result array with stock code as key
*/
$result[$stock] = $data;
}
return $result;
}
}
Link to comment
Share on other sites

This looks like an answer that will work:

http://stackoverflow.com/questions/10236166/does-file-get-contents-have-a-timeout-setting

 

But you could use PHP's cURL library instead of file_get_contents(). cURL is better suited for accessing files from remote servers.

Link to comment
Share on other sites

I tried playing around with the curls first but i dont really understand it just yet and am limited on time at the moment so i chose to use this.

 

ini_set('default_socket_timeout', 900); // 900 Seconds = 15 Minutes

 

I dont know if I put it in the right place as Im still getting the same error.

 

<?php
/**
* Class to fetch stock data from Yahoo! Finance
*
*/
class YahooStock {
/**
* Array of stock code
*/
private $stocks = array();
/**
* Parameters string to be fetched
*/
private $format;
/**
* Populate stock array with stock code
*
* @param string $stock Stock code of company
* @return void
*/
public function addStock($stock)
{
$this->stocks[] = $stock;
}
/**
* Populate parameters/format to be fetched
*
* @param string $param Parameters/Format to be fetched
* @return void
*/
public function addFormat($format)
{
$this->format = $format;
}
/**
* Get Stock Data
*
* @return array
*/
public function getQuotes()
{
$result = array();
$format = $this->format;
foreach ($this->stocks as $stock)
{
/**
* fetch data from Yahoo!
* s = stock code
* f = format
* e = filetype
*/
ini_set('default_socket_timeout', 900); // 900 Seconds = 15 Minutes
$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=$format&e=.csv");
/**
* convert the comma separated data into array
*/
$data = str_getcsv($s, ",", '"');
/**
* populate result array with stock code as key
*/
$result[$stock] = $data;
}
return $result;
}
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...