1

When I type into any browser's address bar : https://username:password@www.example.com/Protected/Export/MyFile.zip, the file gets downloaded normally.

Now I'm trying to do the same with PHP : connect to the remote password-protected file and download it into a local directory (like ./downloads/).

I've tried several ways with PHP (ssh2_connect(), copy(), fopen(), ...) but none were successful.

$originalConnectionTimeout = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', 3); // reduces waiting time

$connection = ssh2_connect("www.example.com");

// use $connection to download the file

ini_set('default_socket_timeout', $originalConnectionTimeout);
if($connection !== false) ssh2_disconnect($connection);

Output : "Warning: ssh2_connect(): Unable to connect to www.example.com on port 22 [..]"

How can I download this file with PHP and store it on a local directory ?

2 Answers2

3

When accessing an url like

https://username:password@www.example.com/Protected/Export/MyFile.zip

you're using HTTP Basic Auth, which sends the Authorization HTTP header. This has nothing to do with ssh, hence you cannot use ssh2_connect().

To access this with php, you could use curl:

$user = 'username';
$password = 'password';
$url = 'https://www.example.com/Protected/Export/MyFile.zip';

$curl = curl_init();
// Define which url you want to access
curl_setopt($curl, CURLOPT_URL, $url);

// Add authorization header
curl_setopt($curl, CURLOPT_USERPWD, $user . ':' . $password);

// Allow curl to negotiate auth method (may be required, depending on server)
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

// Get response and possible errors
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);

// Save file
$file = fopen('/path/to/file.zip', "w+");
fputs($file, $reponse);
fclose($file);
arkuuu
  • 619
  • 7
  • 23
  • With correct credentials and url, I get a "401 Unauthorized" error : Unauthorized This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required. Apache/2.4.10 (Debian) Server at www.xxxxxx.tld Port 80 –  Aug 16 '19 at 09:41
  • @Frank1999 Could you try `curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");` instead of the `curl_setopt($curl, CURLOPT_HTTPHEADER ...)` line? Like in [this answer](https://stackoverflow.com/a/20064360/4946451) – arkuuu Aug 16 '19 at 10:13
  • And if that still does not work, try adding `curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);`, too. This will allow curl to negotiate which auth way works instead of just using Basic Auth. – arkuuu Aug 16 '19 at 10:21
  • Just tried CURLOPT_USERPWD, but still unauthorized. Also please note that $ch should be $curl (small copy-paste issue). I'll keep on trying with next suggestion (curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);). –  Aug 16 '19 at 10:29
  • 1
    Adding curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); seemed to solve the problem. –  Aug 16 '19 at 10:32
  • 1
    there is an error in curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password); //CHANGE VARIABLE NAME TO $curl – Michal - wereda-net Jul 15 '21 at 14:49
1

This is not SSH protocol. It may be something like Apache HTTP Authentication. You can follow and try this guide: HTTP authentication with PHP

Buu Pham
  • 131
  • 1
  • 4