cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

FOLLOWLOCATION kills Apache?

From: Josh Santangelo <josh_at_endquote.com>
Date: Fri, 24 Dec 2004 18:45:53 +0100

I am trying to log in to an ASP.NET site with libcurl/php and scrape
some content. I think that I've got the logging in bit working, but
once the form is submitted I get a 302 and can't follow the redirect.
If I set CURLOPT_FOLLOWLOCATION to 1, my script runs forever and the
Apache process dies. If I turn if off, I get to see the 302 message. I
have duplicated this on both OS X and Debian boxes.

This is the output of curl_version(): libcurl/7.10.5 OpenSSL/0.9.7b
ipv6 zlib/1.1.4

curl_error() is emtpy after the request if FOLLOWLOCATION is off, but I
can't really get at it if it's on.

This is the output of curl_getinfo when it's off:

array(20) {
   ["url"]=>
   string(105) "[removed]"
   ["content_type"]=>
   string(24) "text/html; charset=utf-8"
   ["http_code"]=>
   int(302)
   ["header_size"]=>
   int(509)
   ["request_size"]=>
   int(509)
   ["filetime"]=>
   int(-1)
   ["ssl_verify_result"]=>
   int(0)
   ["redirect_count"]=>
   int(0)
   ["total_time"]=>
   float(0.464)
   ["namelookup_time"]=>
   float(0)
   ["connect_time"]=>
   float(0)
   ["pretransfer_time"]=>
   float(0.001)
   ["size_upload"]=>
   float(432)
   ["size_download"]=>
   float(8715)
   ["speed_download"]=>
   float(18782.327586207)
   ["speed_upload"]=>
   float(931.03448275862)
   ["download_content_length"]=>
   float(8715)
   ["upload_content_length"]=>
   float(432)
   ["starttransfer_time"]=>
   float(0.001)
   ["redirect_time"]=>
   float(0)
}

Here's my code, though I doubt it's terribly relevant:

// make a curl handle and set its options. taken mostly from comments
on php.net/curl.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // this seems to kill
apache entirely
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/curlcookie_' . time());
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE
5.01; Windows NT 5.0');

// get the login page
curl_setopt($ch, CURLOPT_URL, '[removed]');
$html = curl_exec($ch);

// extract the viewstate from the login page so we can post it back
$matches = array();
preg_match('/<input type="hidden" name="__VIEWSTATE" value="([^"]*?)"
\/>/', $html, $matches);
$viewstate = $matches[1];
$viewstate = urlencode($viewstate);

// post back to the login page and attempt to log in
$postfields = 'Username=' . USERNAME . '&Password=' . PASSWORD .
'&Login=Login&__VIEWSTATE=' . $viewstate;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Referer:
https://idd.infocasa.com/idd.infocasa.com_ssl/Login.aspx?
ReturnUrl=%2fidd.infocasa.com_ssl%2fdefault.aspx',
        'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: ' . strlen($postfields)
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

$html = curl_exec($ch);

var_dump(curl_version());
var_dump(curl_error($ch));
var_dump(curl_getinfo($ch));
curl_close($ch);

print($html);

thanks,
-josh
Received on 2004-12-24