Getting stock quote

In this tutorial I will show you how you can get any stock quotes from yahoo and display it on your own site.

Getting stock quote

If you want to display the actual stock quote on your own site then Yahoo finance pages are a good source. You can select the company you need and visit the URL. We will use this information to fetch the html code of the page and find, cut and paste the necessary part.

 

Step 1. 

 

First of all we need to get the correct URL where we can find what we need. In case of Yahoo it looks like this:

 

    http://finance.yahoo.com/q?s=GOOG 

 

As you can see the URL is quite simple, the only thing we have to know is the company short-code. In this example we will use Google and it's short-code is GOOG.

 

Step 2. 

 

Now we have the URL, so we need to get the html code of it in our script. We can open the URL with a simple fopen() function call and as in case of normal files using fread() we load the complete page content into a variable. Let's put this code into a separate function which will look like this: 

 

<?php
function getStockSite($stockLink){
   
   if (
$fp fopen($stockLink'r')) {
      
$content '';
        
      while (
$line fread($fp1024)) {
         
$content .= $line;
      }
   }

   return 
$content;  
}
?>

 

Step 3. 

 

The next step is to process the page content and find the relevant code part in it. We need 2 data from the code.

  1. Company complete name
  2. The actual stock quote

First we need to analyze the source code of the actual page. As you will see we can find special marks which help us to get the correct part of the code. Before all of this string processing we make a very simple check against the content variable to be sure it is filled. We can say for this page if it is smaller than 1000 bytes then something went wrong before.

If everything is fine then we cut the relevant code and paste it into a result array. At the end we will return this array to the caller.

 

The source code looks like this:

 

<?php
function processStockSite($wurl){
    
    
$wrss getStockSite($wurl);
    
$name  '-';
    
$text  '';
    
    if (
strlen($wrss)>100){
        
// Get company name
        
$spos strpos($wrss,'ygtb')+9;
        
$epos strpos($wrss,'<',$spos);
        if (
$epos>$spos){
            
$name substr($wrss,$spos,$epos-$spos);
        } 
        
        
// Get text
        
$spos strpos($wrss,'ET</span>:',$spos)+3;
        
$spos strpos($wrss,'<big>',$spos);
        
$epos strpos($wrss,'</td>',$spos);
        if (
$epos>$spos){
           
$text substr($wrss,$spos,$epos-$spos);
        } else {
           
$text '-';
        }

    }
    
    
$result['name']  = $name;
    
$result['text']  = $text;
    
    return 
$result;
    
}
?>

 

Step 4.

The last step is to call the above defined function and print out the result on your page.

You can find a stock quote script on this site and on the next page you will find the complete source code of this tutorial.

 

[newpage=Complete code]

 

<?php

function getStockSite($stockLink){
   
   if (
$fp fopen($stockLink'r')) {
      
$content '';
        
      while (
$line fread($fp1024)) {
         
$content .= $line;
      }
   }

   return 
$content;  
}

function 
processStockSite($wurl){
    
    
$wrss getStockSite($wurl);
    
$name  '-';
    
$text  '';
    
    if (
strlen($wrss)>100){
        
// Get company name
        
$spos strpos($wrss,'ygtb')+9;
        
$epos strpos($wrss,'<',$spos);
        if (
$epos>$spos){
            
$name substr($wrss,$spos,$epos-$spos);
        } 
        
        
// Get text
        
$spos strpos($wrss,'ET</span>:',$spos)+3;
        
$spos strpos($wrss,'<big>',$spos);
        
$epos strpos($wrss,'</td>',$spos);
        if (
$epos>$spos){
           
$text substr($wrss,$spos,$epos-$spos);
        } else {
           
$text '-';
        }

    }
    
    
$result['name']  = $name;
    
$result['text']  = $text;
    
    return 
$result;
    
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Get Stock tutorial</title>
</head>
<body>
   <?php 
      
// Get stock data
      
$data processStockSite('http://finance.yahoo.com/q?s=GOOG'); 
      echo 
$data['name']."<br/>".$data['text']; 
   
?>
</body>