note 95943 deleted from function.curl-setopt by danbrown
| From: | danbrown@php.net | Date: | Sun, 09 Jan 2011 16:47:25 +0000 |
| Subject: | note 95943 deleted from function.curl-setopt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175564@lists.php.net to get a copy of this message | ||
Note Submitter: achatov at google dot com
----
I would like to improve redirect function. Actually it has to catch also a redirection by meta
refresh tag.
<?php
function redirect_exec($ch, $curlopt_header = false) {
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
$http_code = $info['http_code'];
if ($http_code == 301 || $http_code == 302 || $http_code == 303) {
list($header) = explode("\r\n\r\n", $data, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed['host'])) {
curl_setopt($ch, CURLOPT_URL, $url);
return redirect_exec($ch);
}
}
elseif($http_code == 200){
$matches = array();
preg_match('/(<meta
http-equiv=)(.*?)(refresh)(.*?)(url=)(.*?)[\'|"]\s*>/', strtolower($data),
$matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed['host'])) {
curl_setopt($ch, CURLOPT_URL, $url);
return redirect_exec($ch);
}
}
return $info['url'];
}
?>