0

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.

1
  • Guzzle is a HTTP client, hence relies on correct HTTP headers to function as excepted. As you noted, the redirect is done via JS - so you need to parse the result and manually extract the target URL. Commented Apr 12, 2015 at 11:01

3 Answers 3

3

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:

Sign up to request clarification or add additional context in comments.

Comments

0

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);

1 Comment

This is what I've already did. But it doesn't work (the curl approach)
0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.