Using file_get_contents() / cURL or API to get financial data?

时间:2016-02-12 22:08:04

标签: php json api yahoo google-finance

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 x = np.zeros((10, 4)) y = np.ones((10, 4)) c = np.array([x[0:3, :], y[0:3, :]]) print c.shape # I get (2, 3, 4) np.reshape(c, (6, 4)) print c.shape # I get (2, 3, 4) with an RegEx to fetch the file_get_contents() 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:

<span>

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 $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])); 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.

1 个答案:

答案 0 :(得分: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 />"; 


      } 
} 

?>