我知道fopen()失败在这里已被大量讨论,但似乎我的问题有一个独特的转折:fopen()失败可能四分之一次,其他时候它工作完美。我收到以下错误:
Warning: fopen(http://download.finance.yahoo.com/d/quotes.csv?s=0005.HK&f=nl1c1p2v&e=.csv)
[function.fopen]: failed to open stream: HTTP request failed! in [...] on line 4
它没有系统地排除主机服务器端的大多数问题来源,这可能是由于我正在开发的服务器,Yahoo Finance?
答案 0 :(得分:1)
使用curl lib代替。 http://php.net/manual/en/book.curl.php
function get_cURL($p_url)
{
$ch = curl_init($p_url);
$agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3";
$header[0] = "Accept: text/xml,text/csv,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// grab URL and pass it to the browser
curl_setopt($ch, CURLOPT_URL, $p_url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$buffer = curl_exec($ch);
$this->info = curl_getinfo($ch);
$this->error = curl_error($ch);
curl_close($ch);
return $buffer;
}