0

I want to extend my portfolio tracker to download the share price for the past two weeks. I can do this from the Yahoo site using the following url: http://real-chart.finance.yahoo.com/table.csv?s=ANZ.AX&a=10&b=5&c=2016&d=10&e=18&f=2016&g=d&ignore=.csv But when I try to insert this in my PHP file I get a can't connect error. Any help would be appreciated. My code for this bit is as follows:

// headers for proxy servers
    $headers = [
        "Accept" => "*/*",
        "Connection" => "Keep-Alive",
        "User-Agent" => sprintf("curl/%s", curl_version()["version"])
    ];      

    // set start and end dates (Months go from 0-11, days from 1 - 31.
    $date_now = strtotime($this_date);
    $day = date("d");
    $month = date("m") - 1;
    $year = date("Y");
    $first_month = $month;
    $first_year = $year;
    $first_day = $day-14;

    if($first_day <= 0) 
    {
        $first_month = $first_month - 1;

        if($first_month == 1)
        {
            if((($year / 4) * 4) != $year)
            {
                $first_day = $first_day + 28;
            }
            else
            {
                $first_day = $first_day + 29;

            }
        }
        elseif($first_month == 3 || $first_month == 5 || $first_month == 8 || $first_month == 10)
        {
            $first_day = $first_day + 30;
        }
        else if($first_month == 11)
        {
            $first_year = $first_year - 1;
            $first_day = $first_day + 31;
        }   
        else
        {
            $first_day = $first_day + 31;
        }
    }
    $prices = [[]];

    // open connection to Yahoo
 $context = stream_context_create([
        "http" => [
            "header" => implode(array_map(function($value, $key) { return sprintf("%s: %s\r\n", $key, $value); }, $headers, array_keys($headers))),
            "method" => "GET" 
        ]
    ]);

// dump($symbol, $first_month, $first_day, $first_year, $month, $day, $year);
 $handle = @fopen("http://real-chart.finance.yahoo.com/table.csv?s={$symbol}&a={$first_month}&b={$first_day}&c={$first_year}&d={$month}&e={$day}&f={$year}&g=d&ignore=.csv", "r", false, $context);

    if ($handle === false)
    {
        // trigger (big, orange) error
        trigger_error("Could not connect to Yahoo!", E_USER_ERROR);
        exit;
    }

The dump of my data for the first share is as follows:

FREE
10
4
2016
10
18
2016
1

Big subtle difference between the link you posted and your code

link: http://real-chart.finance.yahoo.com

code: https://real-chart.finance.yahoo.com

ADDENDUM

"FREE" is not an valid stock symbol, so calling this API with the symbol "FREE" will return a 404.

5
  • Hi, I corrected the http but get the same result. Is there something wrong with my header? Nov 19 '16 at 3:53
  • Is there some way of seeing what message actually goes out when the @fopen is executed? Nov 19 '16 at 4:01
  • I've also tried using ichart.finance.yahoo.com/table.csv?... which works fine as a URL but not from my php code. Nov 19 '16 at 4:40
  • Assuming $symbol is set properly, this code connects without error. The problem is "FREE". Try the posted link, replacing "ANZ.AX" with "FREE". If you remove the @ from the fopen call, and it fails, it will show an error message with more info. Nov 19 '16 at 13:43
  • Thank you. I hadn't tweaked that FREE was an artificial share name so doesn't appear in the historic data. Nov 20 '16 at 3:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .