Investing Stock Data Retrieval in PHP


In this article I will show how to CURL with PHP to get data quote stock exchange,. There are free web service which provide investing stock quote data. You can see it in http://www.webservicex.net/WS/WSDetails.aspx?CATID=2&WSID=9 . It’s free  but it has limitation , the data has delay about 20 minutes. The data which is got after we input the parameters is in formed xml.  So we must parse it to get data which we need and formed it in the way which we want.

  1. Make Client To Process Web Service

function getstockxml($symbol)

            {             $url="http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=" . $symbol;              $curlHandle = curl_init(); // init curl   $apiCallUrl = $url;      curl_setopt($curlHandle, CURLOPT_URL, $apiCallUrl); // set the url to fetch   curl_setopt($curlHandle, CURLOPT_HEADER, 0);   curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);   curl_setopt($curlHandle, CURLOPT_TIMEOUT,120);   $content = curl_exec($curlHandle);   curl_close($curlHandle);   return $content;             }

  1. Parse the XML and form the Result in Good GUI

To parse the data in xml, you can use simplexml which  has been provided by PHP. 

function parsestockxml($xmlstr)

                {                                $dom = new domDocument;$dom->loadXML($xmlstr);if (!$dom) {     echo ‘Error while parsing the document’;     exit;                }                                $s = simplexml_import_dom($dom);                //$xml = new SimpleXMLElement($s);                                echo "<table border=1>";                                echo "<tr><td>No</td><td>Stock Code</td><td>Stock Code</td><td>Last</td><td>Change</td><td>Date</td><td>Time</td></tr>";                                $count = 0;                                echo $xml;                                foreach ($s->StockQuotes->Stock as $stockdata)                                 {                                                $count++;                                                                                                echo "<tr><td>" . $count . "</td><td>". $stockdata->Symbol . "</td><td>" . $stockdata->Name . "</td><td>". $stockdata->Last ."</td><td>". $stockdata->Change . "</td><td>". $stockdata->Date . "</td><td>". $stockdata->Time ."</td></tr>";                                }

                                echo "</table>";

3.   Try the web service client

To try the web service we must know the symbol of stock data which we want to know. Here is the example :

No

Symbol Company
1 GOOG Google
2 YHOO Yahoo
3 MSFT Microsoft Cp
4 SBUX StarBuck Cp

Here the all source code :

http://www.gunungpring.com/wordpress/wp-content/uploads/2007/05/investingstock.zip

Leave a Reply




This some posting which are related to this article :
  • Stock Program in Handphone
  • Indonesian Stock Analyze
  • Investing Stock
  • How To Pass Rapidshare Download Time Limit
  • Free Stock Ticker in Yahoo Finance
  • just read related article


    Some random post which may be interesting :
    this article is selected randomly