Vamsi Krishna B
Vamsi Krishna B

Reputation: 11490

cURL , get redirect url to a variable

I'm using curl to fill a form. After completion of the post the other script which handles the form is redirecting to another URL. I want to get this redirect URL into a variable.

Upvotes: 26

Views: 66368

Answers (6)

MUHINDO
MUHINDO

Reputation: 1213

Try this, thank me later

           $fp = fopen($public_path . '/' . $file_name, 'wb');
            curl_setopt($ch, CURLOPT_FILE, $fp); 
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
            //if you are downloading large file, you can now get its exetension from $redirect_url

Upvotes: 0

JWprogrammer
JWprogrammer

Reputation: 41

Hi. There is a new easy way.

If the CURLOPT_FOLLOWLOCATION option is disabled (You are not being redirected automatically)

👍 You can use CURLINFO_REDIRECT_URL: redirect URL found in the last transaction

$redirect_url = curl_getinfo($ch, CURLINFO_REDIRECT_URL);

Or if the CURLOPT_FOLLOWLOCATION option is enabled (Auto redirect)

You can use CURLINFO_EFFECTIVE_URL: last requested URL (in this case is the last redirect URL)

$redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

Upvotes: 1

EGL 2-101
EGL 2-101

Reputation: 1265

Easy way to find the redirected url (if you don't want to know in advance)

$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

Upvotes: 50

nico limpika
nico limpika

Reputation: 81

Here I get the resource http headers then I parse the headers out into an array $retVal. I got the code for parsing the headers from here (http://www.bhootnath.in/blog/2010/10/parse-http-headers-in-php/) You could also use http://php.net/manual/en/function.http-parse-headers.php if you have (PECL pecl_http >= 0.10.0)

        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        // Getting binary data
        $header = curl_exec($ch);
        $retVal = array();
        $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
        foreach( $fields as $field ) {
            if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
                $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
                if( isset($retVal[$match[1]]) ) {
                    $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                } else {
                    $retVal[$match[1]] = trim($match[2]);
                }
            }
        }
//here is the header info parsed out
echo '<pre>';
print_r($retVal);
echo '</pre>';
//here is the redirect
if (isset($retVal['Location'])){
     echo $retVal['Location'];
} else {
     //keep in mind that if it is a direct link to the image the location header will be missing
     echo $_GET[$urlKey];
}
curl_close($ch);

Upvotes: 8

Delta
Delta

Reputation: 4328

You may want to set the CURLOPT_FOLLOWLOCATION to true.

Or set the CURLOPT_HEADER to true and then use regexp to get the Location header.

Upvotes: 3

RobertPitt
RobertPitt

Reputation: 57268

You would use

curl_setopt($CURL, CURLOPT_HEADER, TRUE);

And parse the headers for the location header

Upvotes: 38

Related Questions