Jump to content

Getting yahoo quotes using PHP


sunnysideup

Recommended Posts

I'm trying to get quotes from yahoo finance into my articles (of companies) into my joomla site. I have articles on 100 companies. Now i need to get quotes for these companies in these articles.

 

I use this script for getting quotes:

 

<?php

 

//this is the url of our csv file

$url = "http://in.finance.yahoo.com/d/quotes.csv?s=GOOG&f=sl1d1t1c1ohgvwmj5j6k4k5t7m3m7m8m4m5m6npbaa5a2kj&e=.csv";

 

//open it for reading

$fp = fopen($url , "r");

 

//if no connection exists display error message

if (!fp) {

    echo "could not connect to the site";

}else {

    //store the csv file info in the array $data

    $data = fgetcsv($fp,1000,",");

    //close the file

    fclose($fp);

?>

    <table>

    <tr><td>Last Trade</td><td><?php echo $data[1] ?></td></tr>

    <tr><td>date</td><td><?php echo $data[2] ?></td></tr>

    <tr><td>time</td><td><?php echo $data[3] ?></td></tr>

    <tr><td>change</td><td><?php echo $data[4] ?></td></tr>

    <tr><td>open</td><td><?php echo $data[5] ?></td></tr>

    <tr><td>high</td><td><?php echo $data[6] ?></td></tr>

    <tr><td>low</td><td><?php echo $data[7] ?></td></tr>

    <tr><td>Volume</td><td><?php echo $data[8] ?></td></tr>

    <tr><td>52 Week Range</td><td><?php echo $data[9] ?></td></tr>

    <tr><td>Days Range</td><td><?php echo $data[10] ?></td></tr>

    <tr><td>Change From 52-wk Low</td><td><?php echo $data[11] ?></td></tr>

<tr><td>% Chg From 52-wk Low</td><td><?php echo $data[12] ?></td></tr>

<tr><td>Change From 52-wk High</td><td><?php echo $data[13] ?></td></tr>

<tr><td>% Change From 52-wk High</td><td><?php echo $data[14] ?></td></tr>

<tr><td>Ticker Trend</td><td><?php echo $data[15] ?></td></tr>

<tr><td>50-day Moving Avg</td><td><?php echo $data[16] ?></td></tr>

<tr><td>Change From 50-day Moving Avg</td><td><?php echo $data[17] ?></td></tr>

<tr><td>Pct Chg From 50-day Moving Avg</td><td><?php echo $data[18] ?></td></tr>

<tr><td>200-day Moving Avg</td><td><?php echo $data[19] ?></td></tr>

<tr><td>Change From 200-day Moving Avg</td><td><?php echo $data[20] ?></td></tr>

<tr><td>Pct Chg From 200-day Moving Avg</td><td><?php echo $data[21] ?></td></tr>

<tr><td>eg..</td><td><?php echo $data[22] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Bid</td><td><?php echo $data[24] ?></td></tr>

<tr><td>Ask</td><td><?php echo $data[25] ?></td></tr>

<tr><td>Ask Size</td><td><?php echo $data[26] ?></td></tr>

<tr><td>Average Daily Volume</td><td><?php echo $data[27] ?></td></tr>

<tr><td>52week High</td><td><?php echo $data[28] ?></td></tr>

<tr><td>52 Week Low</td><td><?php echo $data[29] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

<tr><td>Previous Close</td><td><?php echo $data[23] ?></td></tr>

 

 

    </table>

<?php

}

?>

 

These are inserted into joomla article using jumi plugin.

 

 

Now the problem is: If i have to go as per above i'll have to create 100 seperate php files for 100 companies, which is tedious. Moreover if tomorrow yahoo changes its url config, i'll have to redo all 100 files again!!

 

I'm looking for help so that i have to create only one php file and call that script with extending ?symbol=YHOO or ?symbol=GM etc..etc.. 

 

Is it possible? if yes can any gurus help??

 

Thanking all in anticipation.

 

p.s also please advise if fopen is better or a curl based option is better

Link to comment
Share on other sites

I'm lost....can you explain by screenshots

it'll be like reading a book with lots of pictures...which I like

 

 

Also, I'd imagine that Yahoo has it's own API ....open-source...to give people this information.

You shouldn't have to overload their servers with 100 HTTP Requests

 

 

Did a quick google for you

http://www.gummy-stuff.org/Yahoo-data.htm

Link to comment
Share on other sites

Nevermind...I see what you're doing

 

yes, you can make a script from saving you making 100+ PHP pages

look at the $_GET variable

 

 

$quote = $_GET['company'];

$url = "http://in.finance.yahoo.com/d/quotes.csv?s=" . $quote . "&f=sl1d1t1c1ohgvwmj5j6k4k5t7m3m7m8m4m5m6npbaa5a2kj&e=.csv";

 

now you can load you PHP script like this

 

one_of_your_hundreds_of_files.php?company=GOOG

 

poof! solved

Link to comment
Share on other sites

Dear Zanus,

 

The edited script works perfectly when run as standalone, but when I tried inserting using jumi it throws an error:

The file Z:\www\Stockmarket\quotes/quote.php?company=GOOG does not exist or is not readable!

 

I think i should head for jumi forums to ask for help.

 

In the meanwhile if you are aware of jumi and could troh some light it would be helpfull. If i come across a solution i'll post here for ready reference by anyone facing same issue.

 

regards

Sunny

Link to comment
Share on other sites

Dear Zanus,

 

I see that yuo have moved the posts to "## Third Party PHP Scripts" thread. I think as this Joomla related query, other users facing this kind of issue in joomla will easily find it in Joomla thread.

 

Just my point of view, but you would know best as you have been moderating this forum

 

regards

Sunny

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.