php file_get_contents返回404

时间:2011-08-06 06:53:21

标签: php http-status-code-404 file-get-contents

使用以下php代码

$url = "http://ichart.finance.yahoo.com/table.csv?s=EURUSD%3DX&a=6&b=27&c=2011&d=7&e=05&f=2011&g=d&ignore=.csv";
$this->trace("Geting from: $url");
$raw_quote_data = file_get_contents($url);

yahoo.com返回404错误,但网址是正确的,可在我的互联网浏览器下运行。

我还尝试使用stream_context_create为用户代理设置标头,但不起作用。

可能是我托管(000webhost)和雅虎阻止它的问题吗?

1 个答案:

答案 0 :(得分:3)

使用卷曲功能。它对我来说很好。

<?php

$url = "http://ichart.finance.yahoo.com/table.csv?s=EURUSD%3DX&a=6&b=27&c=2011&d=7&e=05&f=2011&g=d&ignore=.csv";
$process = curl_init($url); 
curl_setopt($process, CURLOPT_HEADER, 0); 
curl_setopt($process, CURLOPT_POST, 1); 
curl_setopt($process, CURLOPT_RETURNTRANSFER,1);
curl_setopt($process,CURLOPT_CONNECTTIMEOUT,1);
$response = curl_exec($process); 
curl_close($process); 

echo $response;
?>

我得到了输出:

Date,Open,High,Low,Close,Volume,Adj Close 2011-08-04,1.43,1.43,1.43,1.43,000,1.43 2011-08-03,1.43,1.43,1.43,1.43,000,1.43 2011-08-02,1.42,1.42,1.42,1.42,000,1.42 2011-08-01,1.44,1.44,1.44,1.44,000,1.44 2011-07-29,1.42,1.42,1.42,1.42,000,1.42 2011-07-28,1.43,1.43,1.43,1.43,000,1.43 2011-07-27,1.45,1.45,1.45,1.45,000,1.45