note 95943 modified in function.curl-setopt by danbrown

From: Date: Fri, 29 Jan 2010 18:47:55 +0000
Subject: note 95943 modified in function.curl-setopt by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165500@lists.php.net to get a copy of this message
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']; } ?> --was-- I would like to improve redirect function. Actually it has to catch also a redirection by meta refresh tag. 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']; } http://php.net/manual/en/function.curl-setopt.php

« previous php.notes (#165500) next »