PHP obtains the real address after a short URL jump.

Source: Internet
Author: User
We can use the get_headers () function to obtain the real address after the short URL jump. let's take a look at some examples I have summarized. to obtain a short connection, you need to convert the short connection to a real url. by checking the information, you can find the PH... we can use the get_headers () function to obtain the real address after the short URL jump. let's take a look at some examples I have summarized.

To obtain a short connection, you need to convert the short connection to a real url. by checking the information, we found that PHP provides a function get_headers () to complete this task, first obtain the header information, and then analyze the jump address.

The get_headers () function is used to obtain the http header. the get_headers () function of php is used to obtain all the headers sent by the server in response to an HTTP request. it is certainly no problem to obtain the 301 status.

The code is as follows:

$ Url = 'http: // dwz.cn/29uq}'; $ headers = get_headers ($ url, TRUE); print_r ($ headers ); // output the jump URL echo $ headers ['location'];

// Appendix:

Array (     [0] => HTTP/1.1 302 Moved Temporarily     [Location] => http://www.phprm.com     [Content-Type] => Array         (             [0] => text/html;charset=UTF-8             [1] => text/html;charset=utf-8         )     [Server] => Array         (             [0] => weibo             [1] => BWS/1.0         )     [Content-Length] => Array         (             [0] => 203             [1] => 16424         )     [Date] => Array         (             [0] => Thu, 12 Dec 2013 10:42:25 GMT             [1] => Thu, 12 Dec 2013 10:42:25 GMT         )     [X-Varnish] => 2893360335     [Age] => 0     [Via] => 1.1 varnish     [Connection] => Array         (             [0] => close             [1] => Close         ) )

Let's take a look at the URL before getting a short URL jump. the code is as follows:

$header = get_headers($url, 1);  if (strpos($header[0], '301') || strpos($header[0], '302')) {    if (is_array($header['Location'])) {          return $header['Location'][count($header['Location'])-1];      } else {          return $header['Location'];      }  } else {      return $url;  }


Address:

Reprinted at will, but please attach the article address :-)

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.