Ok so im using yahoos finance api to get stock data but instead of manually adding each stock quote, i want it to use the post title. So for example i want to get stock data for apple inc, so i create a post called aapl (thats their stock quote).
Now i did this thinking it would work, but it doesn’t return any data. Its like this post title isnt being inserted into the url.
$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=<?php the_title(); ?>&f=snl1d1t1cv");
Heres the code im using to get the data from yahoo.
<?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
}
?>
Certainly the following line is incorrect:
That ought to be:
But I can see no reference to that particular function – is it declared elsewhere?
Then, later in your code another line that looks wrong:
Should be:
I’m not sure if this is more or less what you are after, but as I don’t know what the
the_title()
function call does I guessed. I rewrote slightly your code and removed all the comments for brevity/clarity: