douqian6315 2012-12-08 09:57
浏览 114
已采纳

初学者需要简单的PHP脚本

I'm a complete beginner in php (and a first 'poster' here on SO) and seem to be missing something in a small script that I am doing from a tutorial.

What the script is basically suppose to do is get Ticker names from a hosted txt file on the server and output historical prices fetched from yahoo finance.

Everything seems to be working fine except that the content that i get from the getCSVfile function is incorrect (I get the html from the yahoo error page). The fetched URL is however correct and if I type in the targeted URL manually everything works just fine.

It is probably a basic mistake but can't seem to find it. Seems to be related to '' and ""s.

Many thanks in advance for the help Y

<?php 

include("includes/connect.php");

function createURL($ticker){
    $currentMonth = date('n') - 1;
    $currentDay = date('j');
    $currentYear = date('Y');
    $result = 'http://ichart.finance.yahoo.com/table.csv? s='.$ticker.'&a=07&b=19&c=2012&d=11&e=08&f=2012 &g=d&ignore=.csv';
    return (string)$result;
}

function getCSVFile($url, $outputFile){
    $content = file_get_contents($url);
    $content = str_replace('Date,Open,High,Low,Close,Volume,Adj Close','',$content);
    $content = trim($content);
   echo $content; /debugging
  file_put_contents($outputFile,$content);
}

//test debugging - this is where the problem seems to be happening - 
//the URL output is correct as is the getCSVfile but the combination of the two doesnt  work properly//

$test = createURL('GOOG');
echo $test;
getCSVFile($test, "memory.txt");

/code continues...

?>
  • 写回答

2条回答 默认 最新

  • dsqdpn31467 2012-12-08 10:07
    关注

    The problem is that your URL does contain a few spaces which do not belong in there:

    $result = 'http://ichart.finance.yahoo.com/table.csv? s='.$ticker.'&a=07&b=19&c=2012&d=11&e=08&f=2012 &g=d&ignore=.csv';
                                                         ^                                               ^
    

    Try

    $result = 'http://ichart.finance.yahoo.com/table.csv?s='.$ticker.'&a=07&b=19&c=2012&d=11&e=08&f=2012&g=d&ignore=.csv';
    

    instead.

    To notice this kind of error, it is always the best way to copy'n'paste your debug output in the browser, not type it in -- otherwise you will often miss these small, obvious errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么