php取得跳轉位址的方法:先建立一個PHP範例程式碼檔案;然後取得一個短位址;接著透過「get_headers()」函數把頭部資訊取得到;最後分析跳到位址即可。
PHP取得跳轉後的真實位址
取得到一個短連接,需要將短連接轉換成真實的網址,透過查資料,發現PHP 提供了一個函數get_headers() ,可以完成這個任務,先把頭部資訊取得到,然後再分析跳到位址即可:
$url = 'http://t.cn/h5mwx'; $headers = get_headers($url, TRUE); print_r($headers); //输出跳转到的网址 echo $headers['Location'];
結果:
Array ( [0] => HTTP/1.1 302 Found [Date] => Array ( [0] => Mon, 24 Jun 2019 09:35:18 GMT [1] => Mon, 24 Jun 2019 09:35:18 GMT ) [Content-Type] => Array ( [0] => text/html;charset=UTF-8 [1] => text/html ) [Content-Length] => Array ( [0] => 202 [1] => 14615 ) [Connection] => close [Set-Cookie] => Array ( [0] => aliyungf_tc=AQAAAAT06182sQMAe4N7dySC5VJrv03L; Path=/; HttpOnly [1] => BAIDUID=11F195A5E7DFE34FC3BF57618AF40AF5:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [2] => BIDUPSID=11F195A5E7DFE34FC3BF57618AF40AF5; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [3] => PSTM=1561368918; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com ) [Server] => Array ( [0] => nginx [1] => BWS/1.1 ) [Location] => http://www.baidu.com [1] => HTTP/1.0 200 OK [Accept-Ranges] => bytes [Cache-Control] => no-cache [Etag] => "5d0888c3-3917" [Last-Modified] => Tue, 18 Jun 2019 06:46:27 GMT [P3p] => CP=" OTI DSP COR IVA OUR IND COM " [Pragma] => no-cache [Vary] => Accept-Encoding [X-Ua-Compatible] => IE=Edge,chrome=1 ) http://www.baidu.com
很多相關知識,請造訪PHP中文網!
以上是php如何取得跳轉後的真實地址的詳細內容。更多資訊請關注PHP中文網其他相關文章!