need a help in php fetch data code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
north78
Forum Newbie
Posts: 1
Joined: Tue Mar 27, 2007 6:20 am

need a help in php fetch data code

Post by north78 »

JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I am trying to get stock data from a web site whatever the web site but I need from my code to detect if the data I am going to take it exists or not, and if for some reason the destination web form is changed or shifted to another place in the web site so how I can make my code to be flexible and can detect where this web form exists?
my script is designed to pass an argument is the stock keyword and then try to fetch data by using fopen() function but the problem in fopen() it is dealing with static URL address and how I can modify my code to be flexible as I mentioned before?

Anyoen can help me in this

and here is a piece of my code

Code: Select all

<?php
 // -------------------------------------------------------------------------
  //
  // Retrieve information from for example Yahoo finance on a specific stock ticker symbol
  //
  // Usage example:
  //
  //  $s = new ticker();
  //  $s->get("MSFT");
  //  if ($s->lastTrade > $s->open) {
  //    print ("Bill made money today");
  //  }
  //
    //
  //
  // -------------------------------------------------------------------------

  // Prevent multiple inclusions so it is safe to include it pretty much anywhere.
  if (!defined("C_TICKER_PHP")) {
    define("C_TICKER_PHP", 1);

    class ticker {

      var  $ticker;
      var  $lastTrade;
      var  $lastTradeTime;
      var  $change;
      var  $open;
      var  $dayRange;
      var  $high;
      var  $low;
      var  $volume;
      var  $chartURL;

      // ========================================
      // Constructor
      // ========================================

      function ticker() {
        // No constructor requirements
      }

      function get($symbol) {

        // First, clear everything out...
        $this->ticker        = "";
        $this->lastTrade     = "";
        $this->lastTradeTime = "";
        $this->change        = "";
        $this->open          = "";
        $this->dayRange      = "";
        $this->volume        = "";
        $this->chartURL      = "";

        // Get the CSV data from the finance.yahoo.com site, do a quick open/read/close
        // operation. (Not too critical for low volume sites, but higher volume sites
        // will notice some small efficiency increase)

        $fp = fopen("http://quote.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv", "r");

/*here is fetch data from a specific web page by using fopen() so I need to do it dynamically it means for some reasons if this web form is changed so I need  the code can recognize the right page to get data from this is very important
*/ 
        $read = fread($fp, 2048);
        fclose($fp);

        // Remove all the quotes from the information        
        $read = str_replace("\"", "", $read);

        // Parse out the fields on comma boundaries to the $read array
        $read = explode(",", $read);

        // Now, populate the member variables
        $this->ticker        = $read[0];
        $this->lastTrade     = $read[1]; 
        $this->lastTradeTime = $read[2] . " " . $read[3];
        $this->open          = $read[5];
        $this->high          = $read[6];
        $this->low           = $read[7];
        $this->dayRange      = $read[7] . " - " . $read[6];
        $this->volume        = $read[8];
        $this->change        = $this->lastTrade - $this->open;
        $this->chartURL      ="http://ichart.yahoo.com/t?s=$this->ticker";
      }
    }
  }
?>
thank you for your interest


JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

There is no way for a PHP script to detect what a Yahoo developer arbitrarily decides to name the script, unless these changes are being published in some standardized, machine-readable format.
Post Reply