curl跟踪重定向

curl  -Li  https://xxx

i  输出响应头,这样就能定位302之类的

L 跟踪重定向到最后



一种是通过302重定向,

需要解析header里面的内容



<?php
$context = stream_context_create( [
     'ssl' => [
         'verify_peer' => false,
         'verify_peer_name' => false,
     ],
]);

$url = 'https://xxx';
$header = get_headers($url,1,$context);//键值对形式返回
//$header = get_headers($url,0,$context);
var_dump($header);

if (strpos($header[0],'301') || strpos($header[0],'302')) {
         if(is_array($header['Location'])) {
                 //$info = $header['Location'][count($header['Location'])-1];//最后一个
                 $info = $header['Location'][0];//第一个
         }else{
                 $info = $header['Location'];
         }
}
echo $info;




time php getheader.php

real    0m1.913s
user    0m0.018s
sys     0m0.007s

效率偏低



一种是通过2次meta重定向

需要解析内容里面的内容

posted on 2022-11-10 00:37  katago  阅读(406)  评论(0编辑  收藏  举报