我是 php 的完全初学者(也是 SO 上的第一个“海报”),我在教程中做的一个小脚本中似乎遗漏了一些东西。
脚本的基本假设是从服务器上托管的 txt 文件中获取代码名称,并输出从雅虎财经获取的历史价格。
除了我从 getCSVfile 函数获取的内容不正确(我从 yahoo 错误页面获取 html)外,一切似乎都运行良好。然而,获取的 URL 是正确的,如果我手动输入目标 URL,一切正常。
这可能是一个基本错误,但似乎无法找到它。似乎与 '' 和 ""s 有关。
非常感谢您的帮助 对
<?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...
?>
最佳答案
问题是您的 URL 确实包含一些不属于其中的空格:
$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';
^ ^
尝试
$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';
相反。
要注意到这种错误,最好的方法是在浏览器中复制'n'粘贴调试输出,而不是键入它——否则您经常会错过这些小而明显的错误。
关于php - 初学者帮助需要简单的 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776404/