gojakie Posted December 17, 2008 Share Posted December 17, 2008 Dear friends, warm greetings of the season. This is my first post. I am a beginner in php and I am completely unaware of its syntax. I would like to have yahoo stock quotes on an html page. In the past one week, I have been googling a lot and found a working code from somewhere on the Internet. I understood it a bit and made few changes that suits my needs. However, I need some further modification and need help on this. Here is the code: <?php $arr = array ('UNITECH.NS','VIDEOIND.NS','SUZLON.NS','RPL.NS','HDFC.NS','RNRL.NS','HINDALCO.NS'); $url= "http://in.finance.yahoo.com/d/quotes.csv?s=".implode("+", $arr)."&f="."sl1c1p2ohgpvjk"; echo "<table>"; $handle = fopen($url, "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "<tr>"; foreach($data as $d) echo "<td>$d</td>"; echo "</tr>"; } fclose($handle); echo "</table>"; ?> Explanation of the "&f="."sl1c1p2ohgpvjk" line s=Symbol l1=Current market price c1=Change p2=Change % o=Open h=High g=Low p=Previous Close v=Volume j=52 week low k=52 week high I have another few questions. I would really appreciate if somebody can help me. The above code only fetches results of 7 companies. I can get as many as possible but <200. Yahoo has a limit to its csv line. We can extract data of Maximum 200 companies in one csv string. Problem # 1 Instead of adding 200 company codes in the $arr array variable, is it possible to keep it in an external source? May be in a different .php file or in a notepad file? and call it somehow? Problem # 2 In the line while (($data = fgetcsv($handle, 1000, ",")) !== FALSE), what is the importance of the figure 1000? If I increase the company codes from 7 to 200, do I have to increase the $handle figure? Problem # 3 Actually I want to extract data of more than 1218 companies but yahoo limits it to 200 per csv string. How can I modify the code to get all 1218 company's data? In excel vba, to fetch more than 200, I can have a code like given below. How can I modify the php code to get data or 1218 companies in one shot? I am not aware of php syntax. For iMax = 0 To 1000 Step 200 i = 7 + iMax If Cells(i, 1) = "" Then GoTo stopHere End If . . Query to fetch the data . . Putting the first 200 records in the worksheet . . Next iMax All Stock symbols can be downloaded from in.finance.yahoo.com or http://www.traderji.com/attachments/software/2078d1153867956-ystock-realtime-charts-eod-downloads-nse-symbols.zip Thank you Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 17, 2008 Share Posted December 17, 2008 1. include (external php) or fopen and friends (external txt) 2. Read in manual fgetcsv Quote Link to comment Share on other sites More sharing options...
gojakie Posted December 17, 2008 Author Share Posted December 17, 2008 Dear mchl, Thank you for your reply. I am going to read the manual. As I said, I am a beginner, I will try and understand how fgetcsv and fopen functions. In the meantime, I would like to attach list of all 1225 yahoo symbols which I need to refresh on my html page. Please find the attachment. Thanks [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 17, 2008 Share Posted December 17, 2008 Also look into file_get_contents() as it's a quicker way than fopen and friends. Of course, it's not as versatile, but often all you need to do is open/get/close. Quote Link to comment Share on other sites More sharing options...
gojakie Posted December 17, 2008 Author Share Posted December 17, 2008 I am still reading the manual. I am getting the logic but unable to write it due to less knowledge of syntax. I think something like this can help. $fo=fopen(symbols.txt,r); $array=explode("\n",$fo); I open the file and put it in the array. My question is: I have 1225 entries in my symbol.txt file and yahoo accepts less than 200 items in one csv string. How can edit the code to pick only first 200 items in the array, process the table and then take the next 200 until it reaches the end of the file? The code I placed processes 7 records as it has only 7 items in the array. Somebody please help me modify the code so that it fetches all 1225 records by reading items from the symbols.txt file. Thanks Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 17, 2008 Share Posted December 17, 2008 That would look something like this $fo=fopen("symbols.txt","r"); //open file for reading while ($line = fgets($fo)) { //read the file line, by line $array[] = $line; //add line to $array if (count($array) >= 200) { //check if there are 200 items in $array process_array(); //do whatever you need with $array $array = NULL; //empty $array } } if (count($array) > 0) { //there might be some rows left in the array at this point process_array(); //so process them } Quote Link to comment Share on other sites More sharing options...
gojakie Posted December 17, 2008 Author Share Posted December 17, 2008 Thanks again for your reply Few questions: while ($line = fgets($fo)) would read the file line by line. Will it read till the end of file? If yes, it will be of no use as I want only first 200 lines in the array. Similarly $array[] = $line; adds the line to the array. Will will add all 1225 lines as it is the next statement after fgets? We will somehow have to tell that, only fetch the first 200 lines, process the rest of the code then read 201-400 and process the same code again and loop it till the end of the txt file. May be you are giving right answers and I am unable to understand. I am a novice and unable to modify the code that I posted with the logic you have provided purely because I am new to php and don't know the syntax. I started using and understanding php since only a week. Its been over 10 hours I am sitting continuously on this and running around for a solution. Its driving me crazy now. May I ask you to modify the code in my first post that gives the desired output? All I need is an output of 1225 records. A gird of 1225 rows and 11 columns. Thanks Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 17, 2008 Share Posted December 17, 2008 The code I've posted, will read lines from the file and add them to $array. It will also check, if there are 200 lines already in array, and if so, it will call process_array(); function (change it, to whatever you need to do with $array). After that, it will empty $array, and load next 200 lines into it. Repeating until file end. Quote Link to comment Share on other sites More sharing options...
gojakie Posted December 18, 2008 Author Share Posted December 18, 2008 looks like it is working but not fetching data and also getting few errors $url contains fopen(http://in.finance.yahoo.com/d/quotes.csv?s=20MICRONS.NS +3IINFOTEC.NS + Is it because it adds additional SPACE after .NS? or is it because it is not getting in this format '20MICRONS.NS'+'3IINFOTEC.NS'+ of is it becuase of both? Quote Link to comment Share on other sites More sharing options...
gojakie Posted December 18, 2008 Author Share Posted December 18, 2008 Forgot to write one thing. In the output it shows the symbol as 20MICRONS.NS__ 3IINFOTEC.NS__ There are no trailing or leading spaces in my symbols.txt file. Why does it add two underscores after .NS? Is it because of this my data is not fetching? Quote Link to comment Share on other sites More sharing options...
gojakie Posted December 18, 2008 Author Share Posted December 18, 2008 The code is now running but i am still not able to figure out why is it not pulling up the data. May i request you to run the code given below and let me know what is wrong? <?php require_once ( 'settings.php' ); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Stock</title> </head> <?php $fo=fopen("symbols.txt","r"); //open file for reading while ($line = fgets($fo)) { //read the file line, by line $arr[] = $line; //add line to $array if (count($arr) >= 50) { //check if there are 50 items in $array $url= "http://in.finance.yahoo.com/d/quotes.csv?s=".implode("+", $arr)."&f="."sl1c1p2ohgpvjk"; echo "<table>"; $handle = fopen($url, "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "<tr>"; foreach($data as $d) echo "<td>$d</td>"; echo "</tr>"; } fclose($handle); echo "</table>"; $arr = NULL; //empty $array } } ?> Problems with this code: 1. Not pulling up the data. May be because of: a. Stock codes shows differently in the $url variable eg. 20MICRONS.NS__ b. In the $url, there is a leading SPACE after the "+" sign c. In the $url, the stock codes are not given in quotation marks " ' " 2. In this example, I am trying to fetch data of 50 companies in one shot. In this case, different tables are created each consisting data of 50 codes I want all the data in ONE BIG TABLE so that I can use scripts to sort it. 3. It downloads 1200 records properly but doesn't download the last 25??? 4. Is it required to use fclose at the end to close the fopen? and for fgets? Besides solving above problems, I need one enhancement. If I move my mouse in any of the symbol, it should pop up a small page with the image associated with it. For example, if I move my mouse on 20MICRONS.NS, i should get a small script-like page with an image 20MICRONS.jpg Thanks Quote Link to comment Share on other sites More sharing options...
gojakie Posted December 18, 2008 Author Share Posted December 18, 2008 Would really appreciate if somebody can help this novice Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.