Stock Quotes with PHP

Yahoo! Finance provides quotes of virtually any stock in CSV format - all you need to supply is its ticker symbol. This is great for PHP developers who want to display quotes on a finance related website.

Now for a function that makes getting stock quotes feel like child's play:



function getQuote($symbol)
{

$symbol = urlencode( trim( substr(strip_tags($symbol),0,7) ) );
$yahooCSV = "http://finance.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgvpnbaejkr&o=t";

$csv = fopen($yahooCSV,"r");

if($csv)
{
list($quote['symbol'], $quote['last'], $quote['date'], $quote['timestamp'], $quote['change'], $quote['open'],
$quote['high'], $quote['low'], $quote['volume'], $quote['previousClose'], $quote['name'], $quote['bid'],
$quote['ask'], $quote['eps'], $quote['YearLow'], $quote['YearHigh'], $quote['PE']) = fgetcsv($csv, ',');

fclose($csv);

return $quote;
}
else
{
return false;
}
}


The function returns an array of the stock's information. Here is a sample PHP script that uses the above function to display all the available stock information about the Royal Bank of Scotland (Ticker: RBS.L).


$RBSQuote = getQuote("RBS.L"); //Returns an array of stock information

echo "Symbol ".$RBSQuote['symbol']."<br/>";
echo "Last ".$RBSQuote['last']."<br/>";
echo "Date ".$RBSQuote['date']."<br/>";
echo "Timestamp ".$RBSQuote['timestamp']."<br/>";
echo "Change ".$RBSQuote['change']."<br/>";
echo "Open ".$RBSQuote['open']."<br/>";
echo "High ".$RBSQuote['high']."<br/>";
echo "Low ".$RBSQuote['low']."<br/>";
echo "Volume ".$RBSQuote['volume']."<br/>";
echo "Previous Close ".$RBSQuote['previousClose']."<br/>";
echo "Name ".$RBSQuote['name']."<br/>";
echo "Bid ".$RBSQuote['bid']."<br>";
echo "Ask ".$RBSQuote['ask']."<br/>";
echo "EPS ".$RBSQuote['eps']."<br/>";
echo "Year High ".$RBSQuote['YearLow']."<br/>";
echo "Year Low ".$RBSQuote['YearHigh']."<br/>";
echo "PE ".$RBSQuote['PE'];



A pretty nifty piece of code that adds extra utility to your website. I like it because it's simple and concise.

A PHP class called PHPQUOTE is available at Booyah Media that is capable of performing the same task as above in a more object orientated way. PHPQUOTE Download


17 comments:

  1. Thanks for the howto. Now i can use yahoo. One question i have, how can give out the currency.

    I have it change to a unordered list, now i not know how can give out the currency after the last Price.

    Thanks for help and write the Howto.

    Regards
    Silvio

    ReplyDelete
  2. This is awesome; thanks for the post! In case you're ever looking for something very similar but on sports lines and scores, let me know...teo at hubblubb dot com

    ReplyDelete
  3. Doesn't work for me....

    ReplyDelete
    Replies
    1. wooo, this is simplest code, you are dumb. matherchod

      Delete
  4. This is great.

    I'm also having trouble displaying currency. Seems like it's likely something do with yahoo's symbols.

    For instance: I'd like to show Canadian dollar in relation to USD (http://finance.yahoo.com/q?s=CADUSD%3DX&ql=0)
    I've tried a lot of variations ie.
    CAD/USD
    CCY: CADUSD=X
    CADUSD=X

    But I think some of the characters are conflicting with the php code. I don't know.

    Thoughts?

    ReplyDelete
  5. evіdent iѕ highly effective іn reducing уour inside track but
    а crucial ambitious with thіѕ preѕcription ԁrug is also
    its side effects. Іf уou ωould like to know more abοut these
    effective weight losѕ tablets phen375 then ѵіsit: topdiеtingрills.
    Alwаys look for medical evidеncе that can support
    official item claims.

    ReplyDelete
  6. In addition to this, fat burning agents may also
    be an excellent help. We have other medical disorder. Missing some meals and eating high
    calorie snacks are a guaranteed method to hamper your metabolism.


    Stop by my website :: phen375 fat burner

    ReplyDelete
  7. Most performers who spend about 20 hours online earn at least $500-1,000+ per
    week. If you are adventurous and enjoy expressing
    your sexuality while entertaining others, webcam entertaining may be the
    perfect fit for you. If you want you can check an adult sex chat website as it is a good starting point in finding that special someone that is there for you.


    my webpage ... xxx webcams

    ReplyDelete
  8. I do not even know how I ended up here, but I thought this
    post was great. I do not know who you are but definitely you're going to a famous blogger if you are not already ;) Cheers!

    Also visit my page :: buy phen375

    ReplyDelete
  9. Merаtol іs a powerful weight lоss pіll thаt has become νеry populaг in thе UK since
    its releаse in December of 2010. Ηowever if you cоmbіne іt with even a tіny bіt of exercising (ѕuсh
    as a 15 ѕеcond walκ) then it truly excells аt helping you lose weight.
    It's also advisable to maintain it from the reach of children, a breastfeeding mother or perhaps a mother must also avoid using the drug.

    Also visit my site - diet pill meratol

    ReplyDelete
  10. Eѵery weeκenԁ і used to pay
    a visit thіs webѕite, аs i ωant enϳoymеnt, since thіs
    this web site conationѕ in fact good funny data toо.


    Have a look at my wеblog :: Instant Payday Loans

    ReplyDelete
  11. You can сеrtainlу seе your expertisе within the article yοu writе.

    The arena hopеѕ for eνen mοrе passionatе wгiters
    like yοu whο aren't afraid to mention how they believe. Always go after your heart.

    Take a look at my blog post: Same Day Payday Loans

    ReplyDelete
  12. What's up to every single one, it'ѕ іn fact a fastiԁіοus for
    mе tο ρay a vіsit thіѕ web
    page, it includеs priceless Infοгmatіon.


    Takе а lοok аt my web site New Bingo Sites

    ReplyDelete
  13. If уou are going for most excellent сontents liκe
    myself, just ρay a quick vіsit this ѕitе daіly
    as it prеsents feature сontеnts,
    thanks

    Fеel frеe to surf tο my homeρage payday loans

    ReplyDelete
  14. Woah! I'm really loving the template/theme of this website. It's simple, yet effective.
    A lot of times it's tough to get that "perfect balance" between usability and visual appearance. I must say that you've
    done a excellent job with this. In addition, the blog
    loads super fast for me on Internet explorer. Exceptional Blog!


    Review my web blog - phen375 testimonials ()

    ReplyDelete
  15. How can i display Indian Stock values...?

    ReplyDelete