Get location using curl: Redirect URL

Source: Internet
Author: User

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 = ‘‘;$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 = ‘‘;$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 ....


php gets the redirect URL in several ways:

Get location using curl: Redirect URL

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.