Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cURL and response code 302

2,006 views
Skip to first unread message

bill

unread,
Jul 8, 2013, 11:35:44 AM7/8/13
to
when I have cUrl seet up with CURLOPT_FOLLOWLOCATION =>FALSE,
I am getting a 302, moved temporarily.

How do I find out the URL it is being redirected to ?
The effectiveURL that is returned is the one I sent the request to.

bill

Daniel Pitts

unread,
Jul 8, 2013, 12:02:38 PM7/8/13
to
The "Location" response header contains the desired URL.

See:
<http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3>

bill

unread,
Jul 9, 2013, 10:09:33 AM7/9/13
to
Thanks, is there a way to obtain the Location response header
from cURL ?

bill

Arno Welzel

unread,
Jul 9, 2013, 10:24:29 AM7/9/13
to
Maybe this example in the documentation may help:

<http://www.php.net/manual/en/function.curl-exec.php#80442>



--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de

bill

unread,
Jul 9, 2013, 10:51:57 AM7/9/13
to
actually, suggesting that I look at curlopt_header would have
been more clear.

bill

Arno Welzel

unread,
Jul 9, 2013, 11:20:22 AM7/9/13
to
Maybe - but since it is also neccessary to check the header size to
separate the header from the body I refered to the example. And finally
you got the right hint - didn't you? ;-)

Daniel Pitts

unread,
Jul 10, 2013, 12:44:02 AM7/10/13
to
Actually, suggesting that you RTFM yourself would have been most clear.
I understand not knowing about Location and HTTP spec, and not knowing
where to look for that. You should learn how to find things in
libraries that you're using though.

bill

unread,
Jul 10, 2013, 10:40:01 AM7/10/13
to
I don't know if I got it or not. Perhaps you could be a bit more
clear ?
bill


bill

unread,
Jul 10, 2013, 10:42:29 AM7/10/13
to
Actually, I not only read the manual, I studied it (and the
coments) and still missed curlopt_header.... (as usual) until I
had sent my first message.

Sometimes when you are new to an area it is hard to
find/understand the fine print.

bill


bill

unread,
Jul 10, 2013, 11:09:58 AM7/10/13
to
using:

function curl_getNoFollow($url, array $get = NULL, array $options
= array() ){
$defaults = array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER =>0,
CURLOPT_COOKIEFILE => '/tmp/cookies.txt',
CURLOPT_COOKIEJAR=> '/tmp/cookies.txt',
CURLOPT_HEADER =>TRUE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_FOLLOWLOCATION =>FALSE,
CURLOPT_USERAGENT =>'Mosilla/5.0(Windows NT 6.1,;WOW64,rv21.0)
Gecko/20100101 Firefox/21.0)',
);
$ch = curl_init();
curl_setopt ($ch,CURLOPT_URL, $url);
curl_setopt_array($ch, ($options + $defaults));

$result = curl_exec($ch);

$eurl = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
$http =curl_getinfo($ch,CURLINFO_HTTP_CODE);

$resultx =array($result, $eurl, $http,); // result is an
array($result, $eurl, $http)
curl_close($ch);

return $resultx;
}

I get:
url: http://www.mydomain.com/login/login.cfm, http: 302

note the lack of information about the refer to address, which I
need so I can follow.

Arno Welzel

unread,
Jul 10, 2013, 6:12:13 PM7/10/13
to
bill, 2013-07-10 17:09:

[...]
> using:
>
> function curl_getNoFollow($url, array $get = NULL, array $options
> = array() ){
[...]
> I get:
> url: http://www.mydomain.com/login/login.cfm, http: 302
>
> note the lack of information about the refer to address, which I
> need so I can follow.

A quick & dirty example without any error handling etc. - just to get
the idea:

<?php
$options = array(
CURLOPT_URL => 'http://mydomain.example/foobar',
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_RETURNTRANSFER => true
);

$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$resultcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

if($resultcode == 302)
$redirect = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
else
$redirect = '';

echo 'Effective URL: '.$url.'<br/>';
echo 'HTTP result code: '.$resultcode.'<br/>';
echo 'Redirection: '.$redirect.'<br/>';

curl_close($ch);
?>

See in action at <http://arnowelzel.de/samples/curl-sample.php>

CURLINFO_REDIRECT_URL is not in the documentation but it was added in
PHP 5.3.7. Also see <http://php.net/ChangeLog-5.php>.

If this does not work for you (e.g. because you have an older PHP
version), you have to extract the location header "manually".

HTH

Scott Johnson

unread,
Jul 10, 2013, 7:13:48 PM7/10/13
to
I like to think of the premise that if you don't know what you don't
know, you don't know what there is to know.

Scotty

bill

unread,
Jul 11, 2013, 10:51:19 AM7/11/13
to
On 7/10/2013 6:12 PM, Arno Welzel wrote:

> if($resultcode == 302)
> $redirect = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
> else
> $redirect = '';
>
> echo 'Effective URL: '.$url.'<br/>';
> echo 'HTTP result code: '.$resultcode.'<br/>';
> echo 'Redirection: '.$redirect.'<br/>';
>
> curl_close($ch);
> ?>
>
> See in action at <http://arnowelzel.de/samples/curl-sample.php>
>
> CURLINFO_REDIRECT_URL is not in the documentation but it was added in
> PHP 5.3.7. Also see <http://php.net/ChangeLog-5.php>.
>
> If this does not work for you (e.g. because you have an older PHP
> version), you have to extract the location header "manually".

that helps. The php I have available is 5.3, so hints at
extracting it "manually?"

With thanks and appreciation.

bill

Arno Welzel

unread,
Jul 11, 2013, 3:04:57 PM7/11/13
to
bill, 2013-07-11 16:51:
In the example above you will get the headers in $result.

With curl_getinfo(CURLINFO_HEADER_SIZE) you can also check how long the
header part in $result is (beginning with the first character), so

$headers = substr($result, curl_getinfo(CURLINFO_HEADER_SIZE));

should give you all headers.

HTTP headers are in the form

Header1: Value
Header2: Value
[...]

And the redirection URL is

Location: URL

So try to find the "Location: " part and use the remaining text up to
the line break.

BTW: Using tools like the Firebug AddOn for Firefox help a lot to
understand HTTP headers.

bill

unread,
Jul 12, 2013, 9:58:27 AM7/12/13
to
Thanks much. Yes I do use Firebug and it is helpful. Just can't
use it with cURL.

I am going to try to upgrade to php5 version 5.4 later today.

Again, thanks for the help.

bill

bill

unread,
Jul 12, 2013, 9:59:40 AM7/12/13
to
On 7/10/2013 7:13 PM, Scott Johnson wrote:

>>
>>
> I like to think of the premise that if you don't know what you
> don't know, you don't know what there is to know.
>
> Scotty

exactly and it is even harder when the doc's are not quite up to
date.

bill

kyrp...@gmail.com

unread,
Apr 10, 2014, 1:23:46 PM4/10/14
to
asshole
0 new messages