I ran into the same issue today as well. I changed the source from yahoo to google and the function call is working correctly.
The issue is not with your code but appears to be the site where the .csv files are accessed at Yahoo. If you copy the URL in the error message you can attempt to connect to the location through your browser. However, the message that you will get is: Our engineers are working quickly to solve the issue.
I tested the same scenario using AAPL as the symbol and compared Yahoo and Google as the source. I also added auto.assign = TRUE to the arguments as the default will be changing in Quantmod from TRUE to FALSE.
getSymbols("AAPL", env = sp500, src = "yahoo", auto.assign = TRUE,
from = as.Date("1996-01-02"), to = as.Date("2017-03-10"))
Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, :
cannot open URL 'https://ichart.finance.yahoo.com/table.csv?s=AAPL&a=0&b=02&c=1996&d=2&e=10&f=2017&g=d&q=q&y=0&z=AAPL&x=.csv'
In addition: Warning message:
In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, :
cannot open URL 'https://ichart.finance.yahoo.com/table.csv?s=AAPL&a=0&b=02&c=1996&d=2&e=10&f=2017&g=d&q=q&y=0&z=AAPL&x=.csv': HTTP status was '502 Connection refused'
Changing the source to Google works as expected.
getSymbols("AAPL", env = sp500, src = "google", auto.assign = TRUE,
from = as.Date("1996-01-02"), to = as.Date("2017-03-10"))
> ls(sp500)
[1] "AAPL"
It does not appear that Google has a .csv for the SP500 that can be used as a replacement for the Yahoo ^GSPC.