gpt4 book ai didi

javascript - 帖子标题不会出现在网址中

转载 作者:行者123 更新时间:2023-11-28 06:53:16 25 4
gpt4 key购买 nike

好的,我使用雅虎财经 API 来获取股票数据,但我希望它使用帖子标题,而不是手动添加每个股票报价。例如,我想获取苹果公司的股票数据,所以我创建了一个名为 aapl 的帖子(这是他们的股票报价)。

现在我这样做了,认为它会起作用,但它不返回任何数据。就像这个帖子标题没有插入到网址中一样。

$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=<?php the_title(); ?>&f=snl1d1t1cv");

这是我用来从雅虎获取数据的代码。

<?php
/**
* Class to fetch stock data from Yahoo! Finance
*
*/

class YahooStock {

/**
* Array of stock code
*/
private $stocks = array();

/**
* Parameters string to be fetched
*/
private $format;

/**
* Populate stock array with stock code
*
* @param string $stock Stock code of company
* @return void
*/
public function addStock($stock)
{
$this->stocks[] = $stock;
}

/**
* Populate parameters/format to be fetched
*
* @param string $param Parameters/Format to be fetched
* @return void
*/
public function addFormat($format)
{
$this->format = $format;
}

/**
* Get Stock Data
*
* @return array
*/
public function getQuotes()
{
$result = array();
$format = $this->format;

foreach ($this->stocks as $stock)
{
/**
* fetch data from Yahoo!
* s = stock code
* f = format
* e = filetype
*/
$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=<?php the_title(); ?>&f=snl1d1t1cv");

/**
* convert the comma separated data into array
*/
$data = explode( ',', $s);

/**
* populate result array with stock code as key
*/
$result[$stock] = $data;
}
return $result;
}
}


$objYahooStock = new YahooStock;

/**
Add format/parameters to be fetched

s = Symbol
n = Name
l1 = Last Trade (Price Only)
d1 = Last Trade Date
t1 = Last Trade Time
c = Change and Percent Change
v = Volume
*/
$objYahooStock->addFormat("snl1d1t1cv");

/**
Add company stock code to be fetched


aapl = Apple
*/
$objYahooStock->addStock("<?php the_title(); ?>");


/**
* Printing out the data
*/
foreach( $objYahooStock->getQuotes() as $code => $stock)
{
?>
Code: <?php echo $stock[0]; ?> <br />
Name: <?php echo $stock[1]; ?> <br />
Last Trade Price: <?php echo $stock[2]; ?> <br />
Last Trade Date: <?php echo $stock[3]; ?> <br />
Last Trade Time: <?php echo $stock[4]; ?> <br />
Change and Percent Change: <?php echo $stock[5]; ?> <br />
Volume: <?php echo $stock[6]; ?> <br /><br />

<?php
}
?>

最佳答案

下面这行肯定是不正确的:

$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=<?php the_title(); ?>&f=snl1d1t1cv");

应该是:

$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=".the_title()."&f=snl1d1t1cv");

但是我看不到对该特定函数的引用 - 它是在其他地方声明的吗?

然后,稍后在代码中出现另一行看起来错误的内容:

$objYahooStock->addStock("<?php the_title(); ?>");

应该是:

$objYahooStock->addStock( the_title() );
<小时/>

我不确定这是否或多或少是您所追求的,但因为我不知道 the_title() 函数调用的作用,所以我猜测。为了简洁/清晰,我稍微重写了您的代码并删除了所有注释:

<?php
class YahooStock{
private $stocks;
private $format;

public function __construct(){
$this->stocks=array();
}
public function addStock( $stock=false ){
if( $stock ) $this->stocks[]=$stock;
}
public function addFormat( $format=false ){
if( $format ) $this->format = $format;
}
public function getQuotes(){
$result = array();
foreach( $this->stocks as $stock ){
$s = file_get_contents( "http://finance.yahoo.com/d/quotes.csv?s=".$stock."&f=".$this->format );
$result[$stock] = explode( ',', $s);
}
return $result;
}
}

$oYahoo = new YahooStock;
$oYahoo->addFormat("snl1d1t1cv");
$oYahoo->addStock('msft');
$oYahoo->addStock('amzn');
$oYahoo->addStock('yhoo');
$oYahoo->addStock('goog');


foreach( $oYahoo->getQuotes() as $code => $stock ) {
echo "<pre>";
echo "Code:".$stock[0].PHP_EOL;
echo "Name:".$stock[1].PHP_EOL;
echo "Last Trade Price:".$stock[2].PHP_EOL;
echo "Last Trade Time:".$stock[4].PHP_EOL;
echo "Last Trade Date:".$stock[3].PHP_EOL;
echo "Change and Percent Change:".$stock[5].PHP_EOL;
echo "Volume:".$stock[6];

echo "</pre>";
}
$oYahoo=null;
?>

This outputs the following:
---------------------------

Code:"msft"
Name:"Microsoft Corporation"
Last Trade Price:43.91
Last Trade Time:"4:00pm"
Last Trade Date:"9/24/2015"
Change and Percent Change:"+0.04 - +0.09%"
Volume:27906497

Code:"amzn"
Name:"Amazon.com
Last Trade Price: Inc."
Last Trade Time:"9/24/2015"
Last Trade Date:533.75
Change and Percent Change:"4:00pm"
Volume:"-2.32 - -0.43%"

Code:"yhoo"
Name:"Yahoo! Inc."
Last Trade Price:29.34
Last Trade Time:"4:00pm"
Last Trade Date:"9/24/2015"
Change and Percent Change:"-0.40 - -1.34%"
Volume:19109920

Code:"goog"
Name:"Google Inc."
Last Trade Price:625.80
Last Trade Time:"4:00pm"
Last Trade Date:"9/24/2015"
Change and Percent Change:"+3.44 - +0.55%"
Volume:2242735

关于javascript - 帖子标题不会出现在网址中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com