Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL is not following 301 Redirects, what do I need to do?

I am using the CURL c++ api to get quotes from Yahoo's financial API. The curl api and my code seem to be working fine, however I am getting a "301" redirect message when I tell CURL to visit the url I want. How can I get CURL to follow through to the 301 redirect and get the data I want?

Here is the URL I am using:

http://download.finance.yahoo.com/d/quotes.csv?e=.csv&s=WSF,WSH&f=b2b3s

Here is the response I am getting:

<HEAD><TITLE>Redirect</TITLE></HEAD>
<BODY BGCOLOR="white" FGCOLOR="black">
<FONT FACE="Helvetica,Arial"><B>
 "<em>http://download.finance.yahoo.com/d/quotes.csv?e=.csv&s=WSF,WSH,WSM,WSO,WST,WTI,WTM,WTR,WTS,WTU,WTW,WU,WWE,WWW,WX,WXS,WY,WYN,X,XAA,XCJ,XCO,XEC,XEL,XEL-A,XEL-E,XFB,XFD,XFH,XFJ,XFP,XFR,XIN,XJT,XKE,XKK,XKN,XKO,XL,XL-Y,XOM,XRM,XRX,XVF,XVG,Y,YGE,YPF,YSI,YUM,YZC,ZB-A,ZB-B,ZB-C,ZEP,ZF,ZLC,ZMH,ZNH,ZQK,ZTR,ZZ,ZZC&f=b2b3ccd1d2ghjkk2k3l2l3mm2m3m4m5m6m7m8opst7vw</em>".<p></B></FONT>

<!-- default "Redirect" response (301) -->
</BODY>

Here are my CURL init options

CURL *eh = curl_easy_init();

curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
curl_easy_setopt(eh, CURLOPT_URL, url);
curl_easy_setopt(eh, CURLOPT_PRIVATE, url);
curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);

curl_multi_add_handle(cm, eh);

I didn't post my code, as it is all working, I just need the general method of following through woth the 301's with CURL.

like image 788
MichaelICE Avatar asked Jul 04 '10 15:07

MichaelICE


People also ask

Do not follow redirects curl?

In curl's tradition of only doing the basics unless you tell it differently, it does not follow HTTP redirects by default. Use the -L, --location option to tell it to do that. When following redirects is enabled, curl will follow up to 50 redirects by default.

How do you handle redirects?

If content has moved permanently, use a 301 redirect. If it has moved temporarily, use a 302 redirect. Back button: client-side redirects break the Back button. When trying to use the Back button, visitors will immediately be sent to the URL they were redirected to.

Why is redirection not working?

A URL may not be redirecting for many reasons: Browser cache – Your browser has cached a previous request. Clear your browser cache to get the latest. Server cache – Your site is being cached with some caching software, and this needs to be updated to include your redirect.


1 Answers

In the PHP world, the option is named CURLOPT_FOLLOWLOCATION. I assume the constant names are standardized and come from the same header file, so this should work for you.

curl_easy_setopt(eh, CURLOPT_FOLLOWLOCATION, 1); 

(or whatever "boolean true" is in this context.)

Alternatively, when receiving a 30x status code, you can parse the Location header manually for the new address. Obviously though, the "follow location" option is much easier, as it doesn't require a second request.

like image 123
Pekka Avatar answered Oct 11 '22 06:10

Pekka