Get location using curl: Redirect URL

Source: Internet
Author: User
Tags curl

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);// 返回最后的Locationcurl_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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.