Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

cURL and response code 302

瀏覽次數:2,000 次
跳到第一則未讀訊息

bill

未讀,
2013年7月8日 上午11:35:442013/7/8
收件者:
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

未讀,
2013年7月8日 中午12:02:382013/7/8
收件者:
The "Location" response header contains the desired URL.

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

bill

未讀,
2013年7月9日 上午10:09:332013/7/9
收件者:
Thanks, is there a way to obtain the Location response header
from cURL ?

bill

Arno Welzel

未讀,
2013年7月9日 上午10:24:292013/7/9
收件者:
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

未讀,
2013年7月9日 上午10:51:572013/7/9
收件者:
actually, suggesting that I look at curlopt_header would have
been more clear.

bill

Arno Welzel

未讀,
2013年7月9日 上午11:20:222013/7/9
收件者:
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

未讀,
2013年7月10日 凌晨12:44:022013/7/10
收件者:
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

未讀,
2013年7月10日 上午10:40:012013/7/10
收件者:
I don't know if I got it or not. Perhaps you could be a bit more
clear ?
bill


bill

未讀,
2013年7月10日 上午10:42:292013/7/10
收件者:
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

未讀,
2013年7月10日 上午11:09:582013/7/10
收件者:
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

未讀,
2013年7月10日 下午6:12:132013/7/10
收件者:
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

未讀,
2013年7月10日 晚上7:13:482013/7/10
收件者:
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

未讀,
2013年7月11日 上午10:51:192013/7/11
收件者:
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

未讀,
2013年7月11日 下午3:04:572013/7/11
收件者:
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

未讀,
2013年7月12日 上午9:58:272013/7/12
收件者:
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

未讀,
2013年7月12日 上午9:59:402013/7/12
收件者:
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

未讀,
2014年4月10日 下午1:23:462014/4/10
收件者:
asshole
0 則新訊息