0

URL スクレーパー (名前と説明のみ) を作成しており、301 リダイレクトを処理しようとしています。

現在、ヘッダーを確認し、200 でない場合は、ヘッダー内でリダイレクト先の場所を見つけようとします。私の問題は、array_search が Location の値があるキーを返さないために発生します。

これはコード スニペットです。

if(strpos($url_headers[0], "200") !== false){
        echo "in here";
        return $url;
    }else{
        print_r($url_headers);
        //look for location
        $location_key = array_search("Location: ", $url_headers);
        echo "Location Key: " . $location_key;
        $redirect_string = $url_headers[$location_key];
        $clean_url = str_replace("Location: ", "", $redirect_string);
        return $clean_url;
    }

これの出力は次のとおりです。

Array ( [0] => HTTP/1.0 301 Moved Permanently [1] => Location: http://www.google.com/ [2] => Content-Type: text/html; charset=UTF-8 [3] => Date: Wed, 13 Feb 2013 03:30:00 GMT [4] => Expires: Fri, 15 Mar 2013 03:30:00 GMT [5] => Cache-Control: public, max-age=2592000 [6] => Server: gws [7] => Content-Length: 219 [8] => X-XSS-Protection: 1; mode=block [9] => X-Frame-Options: SAMEORIGIN [10] => HTTP/1.0 200 OK [11] => Date: Wed, 13 Feb 2013 03:30:00 GMT [12] => Expires: -1 [13] => Cache-Control: private, max-age=0 [14] => Content-Type: text/html; charset=ISO-8859-1 [15] => Set-Cookie: PREF=ID=fe86e29432d4e240:FF=0:TM=1360726200:LM=1360726200:S=Wg8VEU7kc7UtcKc-; expires=Fri, 13-Feb-2015 03:30:00 GMT; path=/; domain=.google.com [16] => Set-Cookie: NID=67=KH8Zu8EpKjrhje8nD0lk_868mqvQr9pGwsAsaUuPDD_PRUgohJHoOkdlyYEHWmohUtndyENDJ0oZq8pC1aqOg20anXpUn5btQX5GYM6kYlgMhYxIPajtGp9KymmMDO1Y; expires=Thu, 15-Aug-2013 03:30:00 GMT; path=/; domain=.google.com; HttpOnly [17] => P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." [18] => Server: gws [19] => X-XSS-Protection: 1; mode=block [20] => X-Frame-Options: SAMEORIGIN ) Location Key: {"error":"invalid_url","error_code":null}

私は何を間違っていますか?ユーザー提供のリンクをスクレイピングするときにリダイレクトを処理するより洗練された方法はありますか?

4

3 に答える 3

0

一致するものが見つからない場合、strposはfalseを返すため、次のことを行う必要があります。

if( ! strpos($url_headers[0], "200"))
于 2013-02-13T04:09:35.907 に答える
0
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $a = curl_exec($ch);
        if(preg_match('#Location: (.*)#', $a, $r)){
         $l = trim($r[1]);
         return $l;
        }else{
            return $url;
        }

これはほとんどの場合機能しますが、まだ https へのリダイレクトに問題があります (何らかの理由で二重リダイレクトが必要ですか?)

( http://zzz.rezo.net/HowTo-Expand-Short-URLs.html経由)

于 2013-02-13T05:43:39.470 に答える
0
$url_headers[0] = 'HTTP/1.0 200';
if(strpos($url_headers[0], "200") > 0){
  echo "here";
} else {
  //look for location
   $location_key = getLocation($url_headers);
   echo "Location Key: " . $location_key;
}

function getLocation($data) {
    $url = false;
    foreach($data as $key => $value) {
        if (preg_match("/Location:/", $value)) {
             echo "A match was found.";
            //$url = $matches[1];
            $url = $data[$key];
            break;
        }
    }
    return $url;
}
于 2013-02-13T04:06:09.437 に答える