Web Analytics Made Easy -
StatCounter Getting data from a URL with PHP - CodingForum

Announcement

Collapse
No announcement yet.

Getting data from a URL with PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting data from a URL with PHP

    Is there a way of getting some info from a URL and putting it into a PHP array for further processing, such as inserting into MySQL? Example URL: http://finance.yahoo.com/q?s=QQQ110930C00053000 The data I am interested in is Last Trade, Bid, Ask, etc:

    I would like to use PHP. I presently use a PHP script to get Yahoo's stock and currency quotes, but it doesn't work for option quotes. Which is what the above link is pointed to.


    Thanks
    Last edited by leonardw; Aug 18, 2011, 02:23 AM.

  • #2
    Try the following
    PHP Code:
    $contents = @file_get_contents("http://finance.yahoo.com/q?s=QQQ110930C00053000");
    if (
    $contents)
    {
            
    $pattern '#<span id="yfs_l10_qqq110930c00053000">(?P<last_trade>[^<]+)</span>.+?<span id="yfs_t10_qqq110930c00053000">(?P<trade_time>[^<]+)</span>.+?<span id="yfs_c10_qqq110930c00053000" class="yfi_quote_price">(?P<change>.+?)</span>.+?<td class="yfnc_tabledata1">(?P<prev_close>[^<]+)</td>.+?<td class="yfnc_tabledata1">(?P<open>[^<]+)</td>.+?<td class="yfnc_tabledata1">(?P<bid>[^<]+)</td>.+?<td class="yfnc_tabledata1">(?P<ask>[^<]+)</td>.+?<td class="yfnc_tabledata1">(?P<days_range>.+?)</td>.+?<td class="yfnc_tabledata1">(?P<contract_range>.+?)</td>.+?<span id="yfs_v00_qqq110930c00053000">(?P<volume>[^<]+)</span>.+?<td class="yfnc_tabledata1">(?P<open_interest>[^<]+)</td>.+?<td class="yfnc_tabledata1">(?P<strike>[^<]+)</td>.+?<td class="yfnc_tabledata1">(?P<expire_date>[^<]+)</td>#si';

            if (
    preg_match($pattern$contents$m))
            {
                    
    $lastTrade      $m['last_trade'];
                    
    $tradeTime      $m['trade_time'];
                    
    $change         strip_tags($m['change']);
                    
    $prevClose      $m['prev_close'];
                    
    $open           $m['open'];
                    
    $bid            $m['bid'];
                    
    $ask            $m['ask'];
                    
    $daysRange      strip_tags($m['days_range']);
                    
    $contractRange  strip_tags($m['contract_range']);
                    
    $volume         $m['volume'];
                    
    $openInterest   $m['open_interest'];
                    
    $strike         $m['strike'];
                    
    $expireDate     $m['expire_date'];

            }

    Comment


    • #3
      Thanks gvre.

      Comment

      Working...
      X