Reputation: 23
$url='http://wtion';
$headers = array(
'GET '.$url.' HTTP/1.1',
'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3',
'Accept: text/html',
'Accept-Language: ru,en-us;',
'Accept-Charset: windows-1251,utf-8;',
'Connection: close');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//Массив с HTTP заголовками для передачи на сервер
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Не выводить ответ в браузер. Пусть функция пишет все в переменную.
$site=curl_exec($ch); //В случае успеха - html тест запрошенной страницы. Иначе - false
curl_close($ch);
echo $site;
After running the code , I get this line
<meta http-equiv='Refresh' content='0; url=/animation.php'>
How can I follow the redirect and get the response of /animation.php
?
Upvotes: 1
Views: 3102
Reputation: 1304
Curl cannot follow a meta refresh. Use DOMXml to parse the curl response as long as it's valid, you can check for a refresh return, then process the refresh path appropriately.
$url='http://wtion';
$headers = array(
'GET '.$url.' HTTP/1.1',
'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3',
'Accept: text/html',
'Accept-Language: ru,en-us;',
'Accept-Charset: windows-1251,utf-8;',
'Connection: close');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//Массив с HTTP заголовками для передачи на сервер
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Не выводить ответ в браузер. Пусть функция пишет все в переменную.
$site=curl_exec($ch); //В случае успеха - html тест запрошенной страницы. Иначе - false
curl_close($ch);
$xml = simplexml_load_file($site);
$result = $xml->xpath("//meta[@http-equiv='refresh']");
if (!empty($result)) {
... do stuff to get the final $site value....
}
echo $site
Upvotes: 1
Reputation: 36964
I found an implementation in a comment of get_meta_tags() documentation page.
function sendRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
/*curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'GET '.$url.' HTTP/1.1', // Are you sure about this?
'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3',
'Accept: text/html',
'Accept-Language: ru,en-us;',
'Accept-Charset: windows-1251,utf-8;',
'Connection: close'
));*/
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
$result = false;
$contents = sendRequest($url);
// Check if we need to go somewhere else
if (isset($contents) && is_string($contents))
{
preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match);
if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
{
if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
{
return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
}
$result = false;
}
else
{
$result = $contents;
}
}
return $contents;
}
echo getUrlContents('http://wtion');
Upvotes: 0