Question

I am building a stock analysis website to use stock information such as price history etc. I will implement some of my own algorithms once I get the data but I am getting trouble obtaining the historical data.The website is hosted on the free web-hosting of biz.nf. The connect.php file that is included contains all the needed sql info and connects to the database. I have error checks there so I know thats not the problem. I have also tried to copy/paste the links manually and they work so I am creating the URLs properly. I came across some similar topics on this website and on others, but I do not see a clear solution how to fix it or a clear reason why it is happening. Is yahoo blocking me? Also, I would like to mention that I am quite new to php programming but I have a serious background in C and C++ so please consider this when providing any help, if you can. Thanks in advance!

Here is my code for the index.php file that gets executed

Code:

<html>
  <head>
    <title>StockGrader</title>
  </head>
    <body bgcolor="green"  text="black" link="red">
      <div id="header">
          <hr>
             <h1>Welcome To <b><i>StockGrader</b></i><h1/></hr>

<?php
  include("phpINCLUDES/connect.php");

//function that creates a url from which to download data
  function createURL($ticker) 
  {       $currentMonth = date("n") - 1; //"n" current month as a 1-digit number
          $currentDay = date("j");       //"j" current day as a 1-digit number
          $currentYear = date("Y");      //"Y" current year as a 4-digit number
          echo "current URL is http://ichart.finance.yahoo.com/table.csv?s=$ticker&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&a=3&b=10&c=2013&ignore=.csv";
          echo "\n\n\n";
          return "http://ichart.finance.yahoo.com/table.csv?s=$ticker&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&a=3&b=10&c=2013&ignore=.csv";
  }

//function to download data from a url and store into a file
  function getCSVFile($URL, $outputFile) 
  {
      **$content = file_get_contents($URL);**     //goes to the file located at this link and downloads the full file contents as a string of data

      //replace a string which is the first parameter with the string in the second parameter. the string being replaced is in the third parameter
      $content = str_replace("Date,Open,High,Low,Close,Volume,Adj Close","", $content);

      //removes all white space
      $content = trim($content);

      //write a string into a file
      file_put_contents($outputFile, $content);      
  }

  function fileToDatabase($txtFile, $tableName)
  {
          $file = fopen($txtFile, "r");

          while(!feof($file))
          {
              $line = fgets($file);

              //separates a string 
              $pieces = explode(",", $line);

              //stock variables
              $date = $pieces[0];
              $open = $pieces[1];
              $high = $pieces[2];
              $low = $pieces[3];
              $close = $pieces[4];
              $volume = $pieces[5];
              // $adjclose = $pieces[6];   WE ARE NOT GOING TO USE THIS ONE USUALLY
              $amount_change = $close - $open;
              if($open!=0)
              { $percent_change = ($amount_change/$open)*100; }
              else $percent_change = 99999;


              //check if table exists or not
              $sql = "SELECT * FROM $tableName";
              $result = mysql_query($sql); 

              //if table doesn't exist
              if(!$result) 
              {
                  $sql_2 = "CREATE TABLE $tableName (date DATE , PRIMARY KEY(date), open FLOAT, high FLOAT, low FLOAT, close FLOAT, volume INT, amount_change FLOAT, percent_change FLOAT)";
                  mysql_query($sql_2);
              }

              //insert into table
              $sql_3 = "INSERT INTO $tableName(date, open, high, low, close, volume, amount_change, percent_change) VALUES ('$date','$open','$high','$low','$close','$volume','$amount_change','$percent_change')";
              mysql_query(sql_3);
          }
          fclose($file);
  }

  function main()
  {
    //i don't have this file yet
      $mainTickerFile = fopen("tickerMaster.txt","r");
      while(!feof($mainTickerFile))
      {
          $companyTicker = fgets($mainTickerFile);
          $companyTicker = trim($companyTicker);    //trim whitespace just in case

          $fileURL = createURL($companyTicker);    //create url for each stock ticker
          //create a directory path to each file for each ticker
          $companyTxtFile = "txtFiles/".$companyTicker.".txt";           //I NEED TO CREATE THIS FOLDER ON MY SERVER

          getCSVFile($fileURL, $companyTxtFile);

          fileToDatabase($companyTxtFile, $companyTicker);
      }
  }

  main();
?>


              <p><a href="secondpage.html">Run Script</a></p>
     </div>
 </body>
</html>

This is the error I am getting:

[function.file-get-contents]: failed to open stream: Connection refused in /srv/disk12/1368341/www/nemanja.co.nf/index.php on line 26

I made the text in bold (2 asterisks on each side) where the error is happening.

Also, the server im hosting the website on has allow_url_fopen = 1 If it is the case yahoo.com is blocking me is there a way around this? Is there another way of getting all the historical stock data?

Was it helpful?

Solution

Are you sure your shared hosting allows connections to remote url - Unlikely

Most shared hosts tend not to set allow_url_fopen to true in php.ini

Maybe try using curl instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top