I've having trouble downloading a file which is secured using HTTP digest authentication. I've managed to get a command line version of curl to work:
curl --location -u Myusername:mypassword -C - --digest -k https://www.thefile.com/file.xml.gz > /location/to/download/file.xml.gz
However, when I've attempted to connect using PHP cURL, I just don't get anything, nor have I figured out how to specify where to download the file (if it ever does connect successfully):
$username = "Myusername";
$password = "mypassword";
$url = "https://www.thefile.com/file.xml.gz";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($output);
print_r($info);
Can anyone shed any light on how to conver the above cURL command line version into a PHP version? I'm stuck on what i've done wrong/missing.
Thanks!
To specify output file location you need could create a local file handle to write out the actual file data, to store it locally.
Add the following to your example above...
$filename = "file.xml.gz";
$filehandle = fopen($filename, "w+");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $filehandle);
After the curl_close
fclose($filehandle);
To debug your digest authentication, enable write header. This gives you another file with the headers returned. Generally a 401 unauthorized, with a new nonce value, then a 200 ok when the new digest is sent.
$headerfilename = "file.xml.gz.header";
$headerfilehandle = fopen($headerfilename, "w+");
curl_setopt($ch, CURLOPT_WRITEHEADER, $headerfilehandle);
After the curl_close
fclose($headerfilehandle);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.