PHP: Can CURL follow meta redirects
Yes, but you'll have to do it yourself by parsing the response and looking for things that look like:
<meta http-equiv="refresh" content="5;url=http://example.com/" />
Obeying <meta>
refresh requests is a browser-side thing. Use DOM parsing to look for <meta>
tags with the appropriate attributes in the response cURL gives you.
If you can guarantee that the response is valid XML, you could do something like this:
$xml = simplexml_load_file($cURLResponse);
$result = $xml->xpath("//meta[@http-equiv='refresh']");
// Process the $result element to get the relevant bit out of the content attribute
How follow meta redirect with curl and php?
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');
follow redirects with curl in php
cURL will not follow JS or meta tag redirects.
grab redirected url by using cURL
this page does not use a redirect-scheme that libcurl understands (it uses a html <meta http-equiv="REFRESH"
-redirect, unsupported by libcurl), so libcurl can neither tell you where it is being redirected, nor can libcurl auto-follow the redirect (because libcurl does not understand it)
you need to parse out the redirect url yourself from the HTML, eg
function redirect1($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$data = curl_exec($ch);
$domd=@DOMDocument::loadHTML($data);
$xp=new DOMXPath($domd);
// <META http-equiv="REFRESH" content="0;URL=http://sumai.tokyu-land.co.jp/branz/roppongi4/?iad=daikyo" />
$location=$xp->query("//meta[@http-equiv='REFRESH']")->item(0)->getAttribute("content");
// 0;URL=http://sumai.tokyu-land.co.jp/branz/roppongi4/?iad=daikyo
$location=substr($location,stripos($location,'URL=')+4);
curl_close($ch);
return $location;
}
var_dump(redirect1('https://lions-mansion.jp/MA141070/'));
output:
C:\projects\misc>php re.php
string(57) "http://sumai.tokyu-land.co.jp/branz/roppongi4/?iad=daikyo"
Get cURL to follow redirects
curl
has an option to achieve exactly what you are looking for,
following redirects:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
Just add this line to your curl-options before your execution of it.
As you might expect, this will follow any 301 / 302 redirects and ends up on a site, which doesn't redirect your request any further.
Also, remember that (without having a workaround) this might lead to an infinite loop. (site a redirects to b and b redirects to a).
That said, you should use this option as well:
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
This way, your requests will end after 10 redirects and you don't have to bother with your script running in an endless-loop.
A good source for your further work with diffrent options is the regarding site on php.net
PHP get response for POST after redirect
I managed to get it to return the redirect URL by following this thread:
Curl, follow location but only get header of the new location?
I added the following to my script:
$redirect = curl_getinfo($ch)['redirect_url'];
echo $redirect;
Related Topics
Laravel - Decryptexception: 'The MAC Is Invalid'
How to Run the Bind_Param() Statement in PHP
Need Response Body of Http 500 with File_Get_Contents (Php)
How to Set Short Tag(<) in PHP
Using Gcm to Send Notifications on App, Returns Invalidregistration Error
PHP Session-Based Flash Message
PHP Removing HTML Tags from String
Convert All Node's Attributes into Child Nodes
Remove First 4 Characters of a String with PHP
"Distance" Between Colours in PHP