[英]PHP Array Search returning false when value is in Array
I'm writing a URL scraper (just names and descriptions) and am trying to handle 301 redirects. 我正在写一个URL抓取器(只是名称和描述),并试图处理301重定向。
Right now, I check the headers and if not 200, I try to find the Location to redirect to within the headers. 现在,我检查标题,如果不是200,则尝试在标题中找到要重定向到的位置。 My problem arises because array_search does not return the key in which the Location value is despite me seeing it there. 出现我的问题是因为尽管我在那儿看到array_search都不会返回其中Location值所在的键。
This is the code snippet: 这是代码片段:
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;
}
The output of this is: 输出为:
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}
What am I doing wrong? 我究竟做错了什么? Is there a more elegant way to handle redirects when scraping user-provided links? 抓取用户提供的链接时,是否有更优雅的方式来处理重定向?
$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;
}
如果找不到匹配项,则strpos返回false,因此您需要
if( ! strpos($url_headers[0], "200"))
$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;
}
This works for the most part but still having trouble redirecting to https (they need a double redirect for some reason?) 这在大多数情况下都有效,但仍然无法重定向到https(出于某种原因,它们需要双重重定向吗?)
(via http://zzz.rezo.net/HowTo-Expand-Short-URLs.html ) (通过http://zzz.rezo.net/HowTo-Expand-Short-URLs.html )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.