How to Find Where I Will Be Redirected Using Curl in PHP

How can I find where I will be redirected using cURL in PHP?

To make cURL follow a redirect, use:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Erm... I don't think you're actually executing the curl... Try:

curl_exec($ch);

...after setting the options, and before the curl_getinfo() call.

EDIT: If you just want to find out where a page redirects to, I'd use the advice here, and just use Curl to grab the headers and extract the Location: header from them:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (preg_match('~Location: (.*)~i', $result, $match)) {
$location = trim($match[1]);
}

Get final redirect with Curl PHP

Use curl_getinfo() with CURLINFO_REDIRECT_URL or CURLINFO_EFFECTIVE_URL depending on your use case.

CURLINFO_REDIRECT_URL - With the CURLOPT_FOLLOWLOCATION option disabled: redirect URL found in the last transaction, that should be requested manually next. With the CURLOPT_FOLLOWLOCATION option enabled: this is empty. The redirect URL in this case is available in CURLINFO_EFFECTIVE_URL

-- http://php.net/manual/en/function.curl-getinfo.php

Example:

<?php
$url = 'https://google.com/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$html = curl_exec($ch);

$redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

curl_close($ch);

echo "Original URL: " . $url . "\n";
echo "Redirected URL: " . $redirectedUrl . "\n";

When I run this code, the output is:

Original URL:   https://google.com/
Redirected URL: https://www.google.com/

Waiting to rescue redirect url in PHP / cURL

You are trying to grab a redirection (aka Location) header from the first link, but when you open it with JavaScript disabled in your browser you will see that there is none. In fact, you always get a response code 200 and no Location header.

There actually is a javascript that instantly redirects you to the target URL:

<!doctype html>
<style type="text/css" media="all">
html, body {
height: 100%;
margin: 0; /* Reset default margin on the body element */
}
iframe {
display: block; /* iframes are inline by default */
border: none; /* Reset default border */
width: 100%;
height: 100%;
}
</style>
Redirecionando...
<script type="text/javascript">
document.location.href = "XYZ"
</script>

XYZ= Target link you want to grab, I just removed it here for privacy reasons.

You can grab it like so:

$url = 'http://1.2.3.4:8000/foo/bar/'; // URL removed for privacy

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
curl_close($ch);

$lines = explode("\n", $a);
$j = count($lines);
$ticket = null;
for ($i = 0; $i < $j; $i++) {
if (preg_match('/document.location.href = \"(.*)\"/', $lines[$i], $matches)) {
$ticket = trim($matches[1]);
break;
}
}

var_dump($ticket);

Also, your array looping is kind of complicated for no reason, so you can simplify it by using a foreach:

$ticket = null;
foreach (explode("\n", $a) as $line) {
if (preg_match('/document.location.href = \"(.*)\"/', $line, $matches)) {
$ticket = trim($matches[1]);
break;
}
}

PHP - Using cURL to get the final URL status after redirect

You're looking for
CURLOPT_FOLLOWLOCATION


TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).

from: http://docs.php.net/manual/da/function.curl-setopt.php

If you don't plan to use the option CURLOPT_FOLLOWLOCATION then you must make sure you're analyzign the headers correctly to get the status.
From http://php.net/manual/en/function.curl-getinfo.php you can see

CURLINFO_HTTP_CODE - The last response code.(...)

that means: there can be more than one status code.
i.e with http://airbrake.io/login there are two sent:

HTTP/1.1 301 Moved Permanently
(...)

HTTP/1.1 200 OK
(...)

That means, only 200 is going to be returned, and if you want to get ANY result, your function needs to look like:

 if($httpStatus >= 300 && $httpStatus < 400) {
return getUrlStatus($redirectURL);
} else {
return $httpStatus;
}

How to get lastest redirect url in php using curl

I have used two function to do this thing, its work for me.

function getRedirectUrlss($url){ 
$redirect_url = null;

$url_parts = @parse_url($url);
if (!$url_parts) return false;
if (!isset($url_parts['host'])) return false; //can't process relative URLs
if (!isset($url_parts['path'])) $url_parts['path'] = '/';

$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
if (!$sock) return false;

$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
$request .= 'Host: ' . $url_parts['host'] . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($sock, $request);
$response = '';
while(!feof($sock)) $response .= fread($sock, 8192);
fclose($sock);

if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
if ( substr($matches[1], 0, 1) == "/" )
return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
else
return trim($matches[1]);

} else {
return false;
}

}

function getred($url){
$ch = curl_init($url);
curl_exec($ch);
if (!curl_errno($ch)) {
$info = curl_getinfo($ch);
return $info['redirect_url'];
}
curl_close($ch);
}
$dds=getred($url);
$urls= getRedirectUrlss($dds);
echo $urls;


Related Topics



Leave a reply



Submit