检查标头响应,未设置404返回网址

时间:2019-03-09 05:02:08

标签: php

我有2个网址。 [0]返回404,[1]返回301。尝试取消设置404的网址。

array(2) {
  [0]=>
  string(34) "http://www.myricacity.com//outline"
  [1]=>
  string(50) "http://www.cigr.co.jp/pj/shinchiku/A80297//outline"
}

function response($url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, true);    // 
        curl_setopt($ch, CURLOPT_NOBODY, true);    // 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        $output = curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $httpcode;
    }

但是当我尝试取消设置时。

foreach($redirects as $k => $val){
    if ((response($val) == 404)) {
        if (!in_array($val, $dumpLinks)) {
            $dumpLinks [] = $redirects[$k];
            unset($redirects[$k]);
        }
    }
}

我做var_dump $redirects,但仍然包含两个值。

1 个答案:

答案 0 :(得分:0)

您的代码按预期工作,请尝试以下foreach()以便更好地了解正在发生的事情:

foreach($redirects as $k => $val){

    $httpcode = response($val);
    //Break it out...
    echo "{$redirects[$k]} returned {$httpcode}\r\n";
    //...so that you can see what you're getting

    if ($httpcode == 404) {
        if (!in_array($val, $dumpLinks)) {
            $dumpLinks [] = $redirects[$k];
            unset($redirects[$k]);
        }
    }
}

var_dump($redirects);exit;

预期产量

http://www.myricacity.com//outline returned 404
http://www.cigr.co.jp/pj/shinchiku/A80297//outline returned 301
array(1) {
  [1]=>
  string(50) "http://www.cigr.co.jp/pj/shinchiku/A80297//outline"
}