Reputation: 901
I'm not exactly sure why this isn't working I tried googling it to see if there was a answer a lot of them came up with a foreachloop instead of regular for loops can someone explain to me why this isn't working.
<?php
$list = array("GOOG", "AAP", "MSFT");
for($i=0; $i<3; $i++)
{
echo "why isn't this working <br>";
$fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=sl1ve1x&e=.csv","r");
$data = fgetcsv ($fp, 1000, ",");
$exit = str_replace('"',"",$data[3]);
$exchange = str_replace('"',"",$data[4]);
echo "$data[0] <br>";
echo "$data[1] <br>";
echo "$data[2] <br>";
}
?>
I expected it to print google info 3 times it doesn't if I take fopen out by itself once it works but if I do it multiple times it just doesn't do anything what I expected is
output ex
why isn't this work
Goog
8398
789
why isn't this work
Goog
8398
789
why isn't this work
Goog
8398
789
Upvotes: 0
Views: 755
Reputation: 1
$lists = array("GOOG", "AAP", "MSFT");
$result = curl_multi_get($lists);
foreach($result as $data)
{
$data = explode(',',$data);
echo "<h3>Values for ".$data[0]."</h3>";
echo $data[0]."<brtextmessagedfghjkljhgfdsadfghjkkjhgfdsasdfghjklkjhgfdsasdfghjkkjhgfdsasdfghjkjhgfdsdfghjkkjhgfdsdfghjm,.,mnbvcxsdfghjklkjhgfdsdfghjkllkjhgfdsa/>",
$data[1]."<br/>",
$data[2]."<br/>";
}
Upvotes: 0
Reputation: 46602
Why not use a foreach?
<?php
$lists = array("GOOG", "AAP", "MSFT");
foreach($lists as $list)
{
echo "<h3>Values for ".$list."</h3>";
$fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s=".$list."&f=sl1ve1x&e=.csv","r");
$data = fgetcsv ($fp, 1000, ",");
fclose($fp);
echo $data[0]."<br/>",
$data[1]."<br/>",
$data[2]."<br/>";
}
/* Result
Values for GOOG
GOOG
681.72
1582936
Values for AAP
AAP
80.44
1306227
Values for MSFT
MSFT
29.86
43408272
*/
?>
You may also be interested to know that by looping with fopen is roughly 4.6 times slower then using curl_multi as curl multi will make all the requests simultaneously, here are the results:
Simple curl multi function:
<?php
function curl_multi_get($data) {
$curly = array();
$result = array();
$mh = curl_multi_init();
foreach ($data as $id=>$d) {
$curly[$id] = curl_init();
curl_setopt($curly[$id], CURLOPT_URL, 'http://finance.yahoo.com/d/quotes.csv?s='.$d.'&f=sl1ve1x&e=.csv');
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curly[$id], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curly[$id], CURLOPT_TIMEOUT, 30);
curl_setopt($curly[$id], CURLOPT_AUTOREFERER, true);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $curly[$id]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
return $result;
}
?>
Did Curl multi in 0.50315880775452 seconds
<?php
//Benchmark test 1 curl_multi
$time_start = microtime(true);
//---------------------------------------
$lists = array("GOOG", "AAP", "MSFT");
$result = curl_multi_get($lists);
foreach($result as $data)
{
$data = explode(',',$data);
echo "<h3>Values for ".$data[0]."</h3>";
echo $data[0]."<br/>",
$data[1]."<br/>",
$data[2]."<br/>";
}
//--------------------------------------
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Curl multi in $time seconds\n";
//end first test
//Did Curl multi in 0.50315880775452 seconds
?>
Did fopen() in 2.3253791332245 seconds
<?php
//Benchmark test 2 fopen()
$time_start = microtime(true);
//--------------------------------------
$lists = array("GOOG", "AAP", "MSFT");
foreach($lists as $list)
{
echo "<h3>Values for ".$list."</h3>";
$fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s=".$list."&f=sl1ve1x&e=.csv","r");
$data = fgetcsv ($fp, 1000, ",");
fclose($fp);
echo $data[0]."<br/>",
$data[1]."<br/>",
$data[2]."<br/>";
}
//--------------------------------------
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did fopen() in $time seconds\n";
//end second test
//Did fopen() in 2.3253791332245 seconds
?>
Hope it helps
Upvotes: 2