php获取url重定向代码分享

树叶云

php如何获取url重定向?

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




public function get_redirect_url(){
        error_reporting(0);
        $url = $_GET['url'];
        stream_context_set_default([
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ]
        ]);
        $header = get_headers($url, 1);
        // var_dump($header);
        $result = [];
        if($header){
            if(strpos($header[0], '301') !== false || strpos($header[0], '302') !== false){
                $result['code'] = 302;
                $result['redirect_url'] = $header['location'];
            }else{
                $result['code'] = 200;
            }
        }else{
            $result['code'] = 400;
        }
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

 

 

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/114250.html<

(0)
运维的头像运维
上一篇2025-02-17 19:30
下一篇 2025-02-17 19:31

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注