The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 11:42:39 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: How to capture http redirects in cURL  (Read 3373 times)
hvshah69
Rookie
**
Offline Offline

Posts: 21


View Profile
« on: November 30, 2008, 02:49:52 AM »

I was wondering if there is a way to capture all the redirect URLs in cURL.

I was playing with curl_setopt ($ch, CURLOPT_WRITEHEADER, $header_file);, but it does not capture all the redirect headers.

Is there a functionality similar to LiveHTTPHeaders in cURL?

Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #1 on: December 01, 2008, 02:28:17 PM »

Declare a headerfunc and process any Location: directives with some regex.
Logged

hai
netmktg
Rookie
**
Offline Offline

Posts: 37



View Profile
« Reply #2 on: December 06, 2008, 12:57:16 PM »

I was wondering if there is a way to capture all the redirect URLs in cURL.

My Curl class ran into issues since latest PHP does not do FOLLOWLOCATION when OpenbaseDir restriction is in force; as is True for almost every shared host.

And without the FOLLOW feature, the no. of Curl iterations increased. So, I wrote this function that I am currently using in my Curl class. You use this after curl_exec and pass it the response alongwith the curl url e.g.

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $url);

$response = curl_exec($ch);
$response = curlfollow($response, $url);


Code:
function curlfollow($response, $url)
    {
$parseurl_arr = parse_url($url);
$cur_host_url = $parseurl_arr ['scheme'] .'://' . $parseurl_arr ['host'];
$redirect_url = $this->url;
$cur_url_path = rtrim( dirname($url), '/');
$redirect_response = '';

while (true)
{
$redirect_response .= preg_replace('/\nLocation: ((http:\/\/|https:\/\/){0,1}(.+))/', 'XLocation: \1', $response);
if ( preg_match('/\nLocation: ((http:\/\/|https:\/\/){0,1}(.+))/', $response, $m) < 1 ) { break; }

curl_setopt($this->handle, CURLOPT_REFERER, $redirect_url);
$redirect_url = trim($m[1]);

// Location: has relative path
if ( preg_match('/^(http|https):\/\//', $redirect_url) < 1 ) {

// Relative path starting with a '/' denotes Html Docroot
if ( preg_match('/^\//', $redirect_url) > 0 ) { $redirect_url = $cur_host_url . $redirect_url; }
else { $redirect_url = $cur_url_path .'/'. $redirect_url; }
}

$response = "\nFOLLOW-LOCATION: $redirect_url\n\n";
curl_setopt($this->handle, CURLOPT_POST, 0);
curl_setopt($this->handle, CURLOPT_URL, $redirect_url);
$response .= curl_exec($this->handle);

$cur_url = $redirect_url;
$cur_url_path = rtrim( dirname($redirect_url), '/');
}

return $redirect_response;
    }


Of course, you can mod the function a bit to combine the original curl_exec also into the one function; but I have have kept it in 2 separate statements because of some custom-code in my Curl proxy class
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!