I try to get a page with curl from an IIS-Server which demands user authentication.
The curl_exec returns nothing, CURLOPT_RETURNTRANSFER is true. If CURLOPT_RETURNTRANSFER is false the curl_exec returns true.
What’s the problem? The page is neither after successfull nor after unsuccessful authentication empty.
$username = $_POST["acc"];
$password = $_POST["pw"];
$url = "https://secure.website.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$page = curl_exec($ch);
echo "Error: " . print_r(curl_getinfo($ch)) . "<br>";
curl_close($ch);
echo "<br>Page:<br>" . $page . "<br>---------------------------<br>";
The script output looks like this:
Array ( [url] => https://secure.website.com/
[content_type] => text/html
[http_code] => 302
[header_size] => 435
[request_size] => 137
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 0.187
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0.046
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0.187
[redirect_time] => 0
[certinfo] => Array ( ) )
Error: 1
Page:
---------------------------
curl_error outputs no error. The “Error: 1” at the end of the curl_getinfo is kind of my only hint.
The script works on other websites.
Note the status code, which is a 302 Found, meaning that the page is issuing a redirect. Try:
This will make cURL follow the redirects for you, and should fix this issue.
(
Error: 1is irrelevant, that’s just because you’re doingecho print_r().)