I've already read some other questions regarding the same problem and tried the curl examples there. However these examples don't work for this specific url http://www.prisjakt.no/redirect.php?prisid=182556713
. All I get is the same link with the redirect in it.
Do you have a solution to get url of the followed link, in this case http://www.siba.no/tv-lyd-bilde/hodetelefoner/lukkede-hodetelefoner/sennheiser-momentum-109434
?
A solution using Guzzle would also be fine.
Thank you!
Edit: The problem seems to be that the redirect is made via JS and not with a header.
Guzzle does provide some configuration settings for Redirects in Requests
You could "trap" the redirect by adding an Event Listener to the BeforeEvent like so:
$request = $client->createRequest('GET', $url);
$request->getEmitter()->on('before', function (BeforeEvent $event) {
// do something with the event.
});
$response = $client->send($request);
Then within the listener you can grab the url of the new request by calling:
$event->getRequest()->getUrl()
For example:
$client = new GuzzleHttp\Client();
$request = $client->createRequest('GET', 'http://www.google.com');
$request->getEmitter()->on('before', function (GuzzleHttp\Event\BeforeEvent $e) {
echo $e->getRequest()->getUrl() . PHP_EOL;
});
$response = $client->send($request);
Results in:
Set the CURLOPT_FOLLOWLOCATION
option of cURL to true
to follow redirection(s):
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
For Guzzle, according to their doc ,
By default, Guzzle will automatically follow redirects using the non-RFC compliant implementation used by most web browsers . This means that redirects for POST requests are followed by a GET request. You can force RFC compliance by enabling the strict mode on a request's parameter object:
// Set per request $request = $client->post(); $request->getParams()->set('redirect.strict', true); // You can set globally on a client so all requests use strict redirects $client->getConfig()->set('request.params', array( 'redirect.strict' => true ));
By default, Guzzle will redirect up to 5 times before throwing a
Guzzle\\Http\\Exception\\TooManyRedirectsException
. You can raise or lower this value using the redirect.max parameter of a request object:$request->getParams()->set('redirect.max', 2);
function get_real_url($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_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$html = curl_exec($ch);
$url = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
curl_close($ch);
return $url;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.