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


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

Google Finance

Yahoo Finance

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


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.




$url = ""; 
    $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


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.