初学者需要简单的PHP脚本

初学者需要简单的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...

?>

我是一个完整的php初学者(这里的第一个“海报”)似乎缺失了 我在教程中做的一个小脚本中的东西。 p>

脚本基本上要做的是从服务器上的托管txt文件中获取Ticker名称并输出从中获取的历史价格 雅虎财务。 p>

除了我从getCSVfile函数获取的内容不正确(我从雅虎错误页面获取html)外,一切似乎都运行正常。 然而,获取的URL是正确的,如果我手动输入目标URL,一切正常。 p>

这可能是一个基本错误,但似乎无法找到它。 似乎与''和's有关。 p>

非常感谢帮助 Y p>

 &lt;?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。'&amp; a = 07&amp; b = 19&amp; c = 2012&amp; d = 11&amp; e = 08&amp; f = 2012&amp; g = d&amp; 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); 
} 
 
 //测试调试 - 这就是问题似乎发生的地方 -  
 // URL输出和getCSV文件一样正确但是 两者的组合无法正常工作// 
 
 $ test = createURL('GOOG'); 
echo $ test; 
getCSVFile($ test,“memory.txt”); 
 
 / n / code继续..  。
 
?&gt; 
  code>  pre> 
  div>

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.

Try using urlencode before returning the URL in your createURL function. So the code of that function would be something like this

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 urlencode($result);
}