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

920

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 usingfile_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 usingfile_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 withfile_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.

163

Answer

Solution:

<?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 />"; 


      } 
} 

?>

People are also looking for solutions to the problem: javascript - PHP: Fill block of an image with color and save the information

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.