jb60606 Posted June 1, 2007 Share Posted June 1, 2007 First off, I'm new to PHP. My first project is a stock portfolio/watchlist, and I'm having significant problems with two improvements I made to the script this morning. Reading a txt file into an array, making it a string, then using the string in a URL fopen only reading displaying partial data from a file it is sending it a table A brief rundown of the script: -Script uses cURL to retrieve stock market data/quotes from the internet (the data is delivered in a .csv file). -A separate file (symbols.txt) contains the list of symbols the script will retrieve market data for. These symbols are read from the file, into an array, which is then formatted into a '+' separated string (AMD+IBM+GOOG+SIRI). This string of symbols is used in the URL that cURL uses to retrieve market data. (eg: http://www.example.com/marketdata.csv?s=STRINGyadayadaydaya) -cURL redirects fresh data from that .csv file to a local csv file named data.csv -Market data is extracted from data.csv by the script and placed in an HTML table for viewing. example symbol file (symbols.txt): AMD AAPL MSFT INTC DELL GOOG example data file (data.csv): "TASR","N/A - <b>10.55</b>","+0.01",10.57,10.72"12:00pm" "VSCN","N/A - <b>16.14</b>","-0.13",16.38,16.50"11:59am" "WTS","N/A - <b>38.15</b>","+0.25",37.95,38.50"11:55am" "XNN","N/A - <b>2.62</b>","+0.04",2.54,2.6699"11:32am" The first problem I'm having is extracting the symbols from the file and placing them in the array. $symbols = file("data/symbols.txt", FILE_IGNORE_NEW_LINES); //opens symbols.txt, ignores newlines ("\n") sort($symbols); // Sorts symbols in array $sym = implode('+', $arr); // puts a "+" in between each symbol With that code, it sends the array to a string, with each symbol separated by a plus... as it should... "AMD+AAPL+MSFT+INTC+DELL+GOOG" If I echo out the $sym variable, I will get the string exactly as I want it. However, when I put $sym in the URL, it doesn't seem to translate. I get no stock quotes, and "Missing Format Variable" is printed to data.csv. However, If I declare the $sym variable within the script, instead of retrieving it from the symbols file, it works perfectly: e.g. $sym = array("AMD","AAPL","MSFT","INTC","DELL", "GOOG"); sort($symbols); // Sorts symbols in array $sym = implode('+', $arr); // puts a "+" in between each symbol One workaround I discovered was to separate the symbols in the file using commas, instead of new lines. However, this will not let me sort the array, so it's rather pointless. The 2nd problem will be in the next post. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/53885-a-couple-of-problems-curl-fopen-file-array-etc/ Share on other sites More sharing options...
jb60606 Posted June 1, 2007 Author Share Posted June 1, 2007 2nd problem has been happening ever since I began using cURL, this morning, in place of fopen to retrieve the data file from the vendor(I found that most reputable web hosts disable opening remote URLs, so cURL is a must). As I mentioned in my previous post, after the market data has been retrieved and placed in data.csv, the script then reads data.csv, and prints it's contents to the table. For some reason, it is only displaying some of the data. I have no idea what it could be; I've increased the size field of the @fgetcsv 10 fold, though it has made no difference The code: $m_data = 'data/data.csv'; $curl = curl_init(); $file = fopen("$m_data", "w") or die ("Can't open or write file"); curl_setopt($curl, CURLOPT_URL, "http://www.example.com/quotes.csv?s=" . $sym . "yadayadayda"); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_FILE, $file); curl_exec($curl); curl_close($curl); $handle = fopen("$m_data", "r") or die ("Can't open file"); while (!feof($handle) ) { $data = @fgetcsv($handle, 500, ","); It stops displaying data after the the 36th symbol of the array. Quote Link to comment https://forums.phpfreaks.com/topic/53885-a-couple-of-problems-curl-fopen-file-array-etc/#findComment-266453 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.