-1

I'm fetching market data from both Google Finance and Yahoo Finance but struggeling with an issue.

Google Finance

http://www.google.com/finance/info?q=NASDAQ:GOOG

Yahoo Finance

http://query.yahooapis.com/v1/public/yql?q=select%20LastTradePriceOnly%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22GOOG%22%29%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json

I'm experiencing Yahoo Finance as not trustworthy. If I check a stock directly on the Yahoo Finance website I'll get the correct price, but once I use the link above I'll get another price. I compaired the JSON data for Google and Yahoo Finance. Google Finance is much more accurate, but I want to use Yahoo Finance since they have a lot more stocks to fetch. I checked the Google stock (GOOG) via the Yahoo Finance "API" like 30 minutes before the marked closed. It was showing incorrect price with like 2 dollars. The website showed correct price but not the "API". But both my Google Finance script and the website was showing correct price. Does anyone know how it could be solved? If anyone worked with Yahoo Finance "API" before?

Right now I'm using file_get_contents() with an RegEx to fetch the <span> from both sites. This method is working pretty good and fast, but I want to migrate to using cURL since it's faster. I've built a website that fetches data and it only takes like 1 second.

Here is part of my current code:

$data = file_get_contents('http://www.google.com/finance?q=NASDAQ:GOOG');
$stock = '/\<span id\=\"ref_4420283_l\"\>(.*?)\<\/span\>/';
$price = htmlspecialchars(strip_tags($stock [0]));

If I stick with the code above, is there any chance that Google or Yahoo blocks the connection? Because this code is loading the whole website instead of just the JSON data. I just need to currect price.

Summary

I'm currently using file_get_contents() but need to migrate to cURL if I need to stick with this method. I'm aming to use JSON data from Yahoo Finance according to the link above, but the JSON data doesn't seem to be accurate. The JSON data from Google Finance is perfectly accurate but Google doesn't have all stocks, like Yahoo Finance have.

Have anyone had the same issue with Yahoo Finance? Is there any chance they Google or Yahoo blocks my connection if I choose to stick with file_get_contents() or cURL. This is because the website data is accurate for both Google and Yahoo Finance, but not the JSON data from Yahoo.

I'm grateful for all help I can get.

Treps
  • 780
  • 3
  • 12
  • 28
  • There are a number of APIs available to get this type of financial data; most of the good ones are commercial. Recommending one is out of scope for this site; you'll need to do your own research. –  Feb 12 '16 at 22:11
  • I have found Google "API" and Yahoo "API" but Yahoo doesn't seem to be accurate. I also asked a question, if I can be blocked if using file_get_contents() to fetch data instead on using JSON. Sorry for the long text, just thought I need to be specific to get a specific response. – Treps Feb 12 '16 at 22:17

1 Answers1

1
<?PHP 
$url = "http://www.google.com/finance?q=NASDAQ:GOOG"; 
    $input = @file_get_contents($url) or die("Could not access file: $url"); 
    $regexp = '<span id=\"ref_(.*)\">(.*)<\/span>'; 
    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) { 
      foreach($matches as $match) { 

echo $match[2].","."<br />"; 


      } 
} 

?>
ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Ooh, nice example. You can also use echo `$matches[0][0];` to get the price. But I don't quite understand why it's working. How can `ref_(.*)` get "304466804484872_l" in this case? If you check the source code it's like 63 matches for "ref_", but only 2 matches for "ref_304466804484872_l" (both the same). So I would understand if the RegEx was `ref_(.*)_l` instead. Or is it because the code won't fetch anything in the ` – Treps Feb 13 '16 at 22:34