Ver Mensaje Individual
  #3 (permalink)  
Antiguo 28/02/2016, 03:22
xerifandtomas
 
Fecha de Ingreso: octubre-2010
Ubicación: España
Mensajes: 1.007
Antigüedad: 13 años, 6 meses
Puntos: 123
Respuesta: modificar script yahoo stock

Podría quedar de la siguiente manera
Código PHP:
Ver original
  1. <?php
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&f=sl1d1t1c1ohgv&e=.csv');
  4. curl_setopt($ch, CURLOPT_HEADER, false);
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  6. $output = curl_exec($ch);
  7. /* No cerramos la conexion */
  8. //curl_close($ch);
  9. $contents = explode(',', str_replace('$', '', $output));
  10. /* esto es innecesario */
  11. //{
  12. ?>
  13.  
  14. Apple stock price:  <?php echo $contents[1] ?>
  15.  
  16. <?php
  17. /* esto es innecesario */
  18. //}
  19. ?>
  20.  
  21.  
  22. <?php
  23. /* no volvemos a iniciar curl por que aun esta abierta la conexion */
  24. //$ch = curl_init();
  25. curl_setopt($ch, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=sl1d1t1c1ohgv&e=.csv');
  26. /* esto ya se establecio al iniciar curl */
  27. //curl_setopt($ch, CURLOPT_HEADER, false);
  28. //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  29. $output = curl_exec($ch);
  30. $contents = explode(',', str_replace('$', '', $output));
  31. /* esto es innecesario */
  32. //{
  33. ?>
  34.  
  35. Google stock price: <?php echo $contents[1] ?>
  36. <?php
  37. /* esto es innecesario */
  38. //}
  39. ?>

Y eliminando lo inecesario quedaria algo así:

Código PHP:
Ver original
  1. <?php
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&f=sl1d1t1c1ohgv&e=.csv');
  4. curl_setopt($ch, CURLOPT_HEADER, false);
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  6. $output = curl_exec($ch);
  7. $contents = explode(',', str_replace('$', '', $output));
  8. /* Guardamos el resultado en una variable*/
  9. $Apple_stock_price=$contents[1];
  10.  
  11. curl_setopt($ch, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=sl1d1t1c1ohgv&e=.csv');
  12. $output = curl_exec($ch);
  13. $contents = explode(',', str_replace('$', '', $output));
  14. /* Guardamos el resultado en una variable*/
  15. $Google_stock_price=$contents[1];
  16.  
  17. /*    Ahora que tenemos los resultados en variables podemos mostrarlo en pantalla o
  18.     o tambien hacer cualquier otra operacion con ellos como guardarlos en una BD */
  19. echo ' Precio Apple: '.$Apple_stock_price;
  20. echo ' Precio Google: '.$Google_stock_price;
  21. ?>