【问题标题】:php file_get_contents return 404php file_get_contents 返回 404
【发布时间】:2011-08-06 06:53:21
【问题描述】:

用下面的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 错误,但 url 是正确的并且可以在我的 Internet 浏览器下运行。

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

也许是我的主机问题 (000webhost) 被雅虎屏蔽了?

【问题讨论】:

  • 在我的服务器上运行良好
  • 当我删除 c=2011 它工作得很好......也适用于 2010 年及之前(至少到 2000 年)。也许那个时期的数据不可用,或者时期太短?但这并不能解释为什么它适用于 Zerk

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


【解决方案1】:

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

<?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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 2017-09-30
    • 2021-09-15
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多