SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Invalid argument supplied for foreach()

    Hey guys,

    I just purchased a script from a website and unfortunately their support page doesn't work. So hopefully somebody here is able to help me out.

    When I try to run this script I get this error...

    Code:
    Warning: Invalid argument supplied for foreach() in /nfs/c02/h05/mnt/15401/domains/mariodabrowski.com/html/Examples/stock/ajax-sqt.php on line 22
    Here is the file where the error is occuring...

    Code:
    <?
    
    $use_curl = 1;
    $symbols = "MSFT,GOOG,YHOO";
    
    $quote = array();
    $url = "http://finance.yahoo.com/d/quotes.csv?s=$symbols&f=sl9c6p4&e=.csv";
    
    if($use_curl) {
      $ch = curl_init();
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
      $fcontents = curl_exec($ch);
      curl_close($ch);
    } else {
      $fcontents = @file($url);
    }
    
    //echo "fcontents=$fcontents";
    
    foreach($fcontents as $line) {
      $info = split(",", $line);
      for($i = 0; $i < sizeof($info); $i ++) {
        $info[$i] = trim(str_replace('"', "", $info[$i]));
      }
      $last = $info[1];
      $change = $info[2];
      $changep = $info[3];
      $symbol = $info[0];
      echo "$symbol|$last|$change|$changep\n";
      flush();
    }
    
    echo "$line";
    
    ?>
    Now this only happens when I have $use_curl = 1; set to 1. When it is set to 0 it works but then it doesn't refresh automatically (the ajax doesn't work).

    Does anybody know why I may be receiving this error?

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change
    PHP Code:
    $fcontents = @file($url);
    // to
    $fcontents file($url); 
    so you can see the error message, if any.

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i was going to ask if you had curl installed, but you must if you are getting that error instead of a missing function error

    if you uncomment this
    //echo "fcontents=$fcontents";

    what does it output?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ignore my last post, I thought you said it works when curl was being used.

    That script never would have worked.

    PHP Code:
    // ...
    $fcontents curl_exec($ch);
    //add
    $fcontents preg_split('#\r\n|\r|\n#'$fcontents); 

  5. #5
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    Crmalibu - It just give the same error.

    Galen - you can see the new error message here http://www.mariodabrowski.com/Exampl...k/ajax-sqt.php

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  6. #6
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try changing url to this. it appears the api has moved and curl isnt following the redirect because you dont have CURLOPT_FOLLOWLOCATION set.

    $url = 'http://download.finance.yahoo.com/d/quotes.csv?s=$symbols&f=sl9c6p4&e=.csv';

  7. #7
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Crmalibu - I added that line...

    Code:
      $fcontents = curl_exec($ch);
      $fcontents = preg_split('#\r\n|\r|\n#', $fcontents);
    and here's what happens now...

    http://www.mariodabrowski.com/Exampl...k/ajax-sqt.php

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  8. #8
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    crmalibu - With the latest change here's what happens...

    http://www.mariodabrowski.com/Exampl...k/ajax-sqt.php

    Is there a way to check if cURL is working properly?

    I'm with mediatemple btw.

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  9. #9
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mediatemple has curl installed, i use mediatemple. you have to change the url like i stated above.

  10. #10
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Sorry I forgot to mention I changed the url and here's what I get now...

    http://www.mariodabrowski.com/Exampl...k/ajax-sqt.php

    There is also a javascript file involved here, not sure if that makes any difference...

    Code:
    working = false;
    
    feed = 'http://www.sentosoft.com/products/ajax-sqt.php';
    imagesPath = 'http://www.sentosoft.com/products/ajax-sqt/img/';
    
    
    stocks = new Array();
    stock = new Object();
    
    o = new Stock("Yahoo!", "YHOO", null, null, null);
    stocks[0] = o;
    
    o = new Stock("Microsoft", "MSFT", null, null, null);
    stocks[1] = o;
    
    o = new Stock("Google", "GOOG", null, null, null);
    stocks[2] = o;
    
    function getStock(id) {
      for(i = 0; i < stocks.length; i ++) {
        if(stocks[i].id == id) {
          return stocks[i];
        }
      }
      return null;
    }
    
    function showWatchlist() {
      document.write("<div class=watchlist>");
      showStocks();
      document.write("</div>");
    }
    
    function showStocks() {
      for(i = 0; i < stocks.length; i ++) {
        stock = stocks[i];
        sclass = i &#37; 2 == 0 ? "light" : "dark";
        document.write("<div class=" + sclass + ">");
        showStock(stock);
        document.write("</div>");
      }
    }
    
    function showStock(stock) {
      document.write("<div id=" + stock.id + ">")
      stockHTML = getStockHTML(stock);
      document.write(stockHTML);
      document.write("</div>");
    }
    
    function getStockHTML(stock) {
      s = "<div class=row>";
      s += "<div class=stockName>" + stock.name + "</div>";
      if(stock.last) {
        slast = stock.last;
      } else {
        slast = "N/A";
      }
    
      sicon = "";
      sChangeClass = "";
      if(stock.change) {
        if(stock.change > 0) {
          sicon = "<img src='" + imagesPath + "up.gif'>";
          sChangeClass = "valueUp";
        } else if(stock.change < 0) {
          sicon = "<img src='" + imagesPath + "dn.gif'>";
          sChangeClass = "valueDn";
        } else {      
          sicon = "<img src='" + imagesPath + "st.gif'>";
        }    
      } 
    
      schange = stock.change;  
      schangep = stock.changep;
    
      s += "<div align=center class=stockLast>" + slast + "</div>";
      s += "<div class=stockIcon>" + sicon + "</div>";
      s += "<div align=right class=stockChange><span class=" + sChangeClass + ">" + schange + "</span></div>";
      s += "<div align=right class=stockChangep><span class=" + sChangeClass + ">" + schangep + "</span></div>";
      s += "</div>";
      return s;
    }
        
    
    
    function Stock(name, id, last, change, changep) {
      this.id = id;
      this.name = name;
      this.last = last;
      this.change = change;
      this.changep = changep;
    }
    
    function createRequestObject() {
      var ro;
      var browser = navigator.appName;
      if(browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        ro = new XMLHttpRequest();
      }
      return ro;
    }
    
    var http = createRequestObject();
    
    function sndReq() {
      working = true;
      http.open('get', feed + "?time=" + new Date().getTime());
      http.onreadystatechange = handleResponse;
      http.send(null);
    }
    
    function doUpdate() {
      if(!working) {
        sndReq();
      }
    }
    
    
    function handleResponse() {
      if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();
    
        lines = response.split("\n");
        for(var i = 0; i < lines.length; i ++) {
          line = lines[i];
          if(line.length > 0 && line.indexOf('|' != -1)) {
            update = line.split('|');
            id = update[0];
            last = update[1];
            change = update[2];
            changep = update[3];
            updateStock(id, last, change, changep);        
          }
        }
        setTimeout("doUpdate()", 5000);
        working = false;
      } 
      
    }
    
    function updateStock(id, last, change, changep) {  
      stock = getStock(id);
      if(stock != null) {
        stock.last = last;
        stock.change = change;
        stock.changep = changep;
        s = getStockHTML(stock);
        document.getElementById(id).innerHTML = getStockHTML(stock);
      }  
    }
    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  11. #11
    SitePoint Addict telos's Avatar
    Join Date
    Mar 2005
    Location
    192.168.2.34
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change
    echo "fcontents=$fcontents";
    to
    print_r($fcontents);
    What does that produce?

  12. #12
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks for trying guys, I still have problems.

    Telos - with your method this is what happens...

    http://www.mariodabrowski.com/Exampl...k/ajax-sqt.php

    Even when I change the url I just get a blank response. However when use_curl is 0 it works, but won't refresh in real time.

    Can I send somebody the script so they can see if it will work for them?

    Thanks for your time guys, any help is appreciated.

    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  13. #13
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    post your new code. you've been changing it so much who knows what it looks like by now

  14. #14
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $use_curl 1;
    $symbols "MSFT,GOOG,YHOO";

    $url "http://download.finance.yahoo.com/d/quotes.csv?s=$symbols&f=sl9c6p4&e=.csv";

    if(
    $use_curl) {
      
    $ch curl_init();
      
    curl_setopt ($chCURLOPT_URL$url);
      
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
      
    curl_setopt ($chCURLOPT_CONNECTTIMEOUT5);
      
    $fcontents explode("\n"trim(curl_exec($ch)));
      
    curl_close($ch);
    } else {
      
    $fcontents = @file($url);
    }

    //echo "fcontents=$fcontents";

    foreach($fcontents as $line) {
      
    $info split(","$line);
      for(
    $i 0$i sizeof($info); $i ++) {
        
    $info[$i] = trim(str_replace('"'""$info[$i]));
      }
      
    $last $info[1];
      
    $change $info[2];
      
    $changep $info[3];
      
    $symbol $info[0];
      echo 
    "$symbol|$last|$change|$changep\n";
      
    flush();

    I changed the api url to the new url

    and changed the line

    PHP Code:
      $fcontents explode("\n"trim(curl_exec($ch))); 
    file() creates an array, which your coded needed to work. Explode does just that.

  15. #15
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Galen,

    Thank you for being so patient and taking the time to look through the code.

    The quotes seem to be working now, however the whole ajax auto refresh thing doesn't seem to be working.

    The latest code that Galen posted can be found here...

    http://www.mariodabrowski.com/Exampl...k/ajax-sqt.php

    Anybody know what may be the problem with the auto refresh?

    Oh I thought I'd throw in the website where I purchased the script - http://www.sentosoft.com/ajax-stock-ticker.php

    They have the working product on their page. The only problem is they don't reply to emails and their contact form doesn't work.

    Thank you,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •