2013-02-13 85 views
0

我正在編寫一個URL刮板(只是名稱和說明),並試圖處理301重定向。PHP Array搜索返回值時返回false數組

現在,我檢查標題,如果不是200,我嘗試在標題中找到要重定向到的位置。我的問題出現了,因爲儘管我在那裏看到它,但array_search不返回位置值所在的鍵。

這是代碼片段:

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} 

我在做什麼錯?在抓取用戶提供的鏈接時有沒有更好的方式來處理重定向?

+2

不會我有一個自動的重定向頭解析機制? – 2013-02-13 03:36:22

+0

嘗試實施捲曲,如果存在自動重定向機制,則不會啓動:請參閱http://tryecruit.com/app/app.php?action=new&object=resource&url=google.com – 2013-02-13 03:56:27

回答

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; 
} 
+0

數組密鑰包含「位置:「從網站更改爲網站,這是我第一次嘗試,然後我繼續嘗試使用數組搜索來捕獲適當的值,無論它在哪裏。 – 2013-02-13 05:03:54

+0

現在嘗試爲您更新,它將解決您的http和https問題 – Shridhar 2013-02-13 06:57:38

0

strpos返回false如果它,所以你需要做的

if(! strpos($url_headers[0], "200")) 
+0

問題是,array_search實際上沒有找到「位置:」的關鍵位置,儘管事實上在前幾行中,它被證明具有該位置值 – 2013-02-13 05:03:06

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(他們需要一個沒有找到匹配由於某種原因雙重定向?)

(via http://zzz.rezo.net/HowTo-Expand-Short-URLs.html