Post title wont go in url

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.

Read More
$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
}
?>

Related posts

1 comment

  1. Certainly the following line is incorrect:

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

    That ought to be:

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

    But I can see no reference to that particular function – is it declared elsewhere?

    Then, later in your code another line that looks wrong:

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

    Should be:

    $objYahooStock->addStock( the_title() );
    

    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:

    <?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
    

Comments are closed.