In PHP to get HTTP header information, PHP has a function get_headers (), I used to use this, I heard that efficiency in win, plus the recent research Baidu URL no fruit, wrote curl get redirect URL php code to toss.
I used to use get_headers to get the URL after the jump.
Get_headers's Code
1234567891011 |
//curl的百度百科
$url =
‘http://www.baidu.com/link?url=77I2GJqjJ4zBBpC8yDF8xDhiqDSn1JZjFWsHhEoSNd85PkV8Xil-rckpQ8_kjGKNNq‘
;
$header = get_headers(
$url
,1);
if (
strpos
(
$header
[0],
‘301‘
) ||
strpos
(
$header
[0],
‘302‘
)) {
if
(
is_array
(
$header
[
‘Location‘
])) {
$info =
$header
[
‘Location‘
][
count
(
$header
[
‘Location‘
])-1];
}
else
{
$info =
$header
[
‘Location‘
];
}
}
echo $info
;
|
Now use curl to rewrite the code again.
Curl is required to set curl_setopt and Curl_getinfo to get location: redirect
123456789101112131415 |
//curl的百度百科
$url =
‘http://www.baidu.com/link?url=77I2GJqjJ4zBBpC8yDF8xDhiqDSn1JZjFWsHhEoSNd85PkV8Xil-rckpQ8_kjGKNNq‘
;
$ch = curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
// 不需要页面内容
curl_setopt(
$ch
, CURLOPT_NOBODY, 1);
// 不直接输出
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, 1);
// 返回最后的Location
curl_setopt(
$ch
, CURLOPT_FOLLOWLOCATION, 1);
curl_exec(
$ch
);
$info = curl_getinfo(
$ch
,CURLINFO_EFFECTIVE_URL);
curl_close(
$ch
);
echo ‘真实url为:‘
.
$info
;
|
Because it is also related to the speed of the two code efficiency I have not tested. I use get_headers local test is possible, but upload to server execution error, do not know why?
End ....
Source: http://enenba.com/?post=252
php gets the redirect URL in several ways: http://blog.sina.com.cn/s/blog_5b1acf750102v6iw.html
Get location using curl: Redirect URL