简体   繁体   English

初学者需要简单的PHP脚本

[英]Beginner help need for simple PHP script

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. 我是一个完整的PHP初学者(这里的第一个'海报'在这里),似乎在一个小脚本中遗漏了一些我在教程中做的事情。

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. 脚本基本上假设要做的是从服务器上的托管txt文件中获取Ticker名称,并输出从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). 一切似乎工作正常,除了我从getCSVfile函数得到的内容不正确(我从雅虎错误页面获取html)。 The fetched URL is however correct and if I type in the targeted URL manually everything works just fine. 但是,获取的URL是正确的,如果我手动输入目标URL,一切正常。

It is probably a basic mistake but can't seem to find it. 这可能是一个基本的错误,但似乎无法找到它。 Seems to be related to '' and ""s. 似乎与''和's有关。

Many thanks in advance for the help Y 非常感谢提前帮助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...

?>

The problem is that your URL does contain a few spaces which do not belong in there: 问题是您的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';
                                                     ^                                               ^

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. 在createURL函数中返回URL之前尝试使用urlencode。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM