Sharing large files using 'Dropbox' API
Multi-factor authentication using 'Symworld CPaaS SMS API'
EC site using 'PAY.JP' API
Maps using 'Google Maps Platform' API or 'Leaflet + OpenStreetMap'
Webhook, Sending email via an SMTP server, and other...
<?php
$version = curl_version();
print_r($version['protocols']);
Array ( [0] => dict [1] => file [2] => ftp [3] => ftps [4] => gopher [5] => gophers [6] => http [7] => https [8] => imap [9] => imaps [10] => ldap [11] => ldaps [12] => mqtt [13] => pop3 [14] => pop3s [15] => rtsp [16] => scp [17] => sftp [18] => smb [19] => smbs [20] => smtp [21] => smtps [22] => telnet [23] => tftp )
<?php
// MD5 Fingerprint
$md5_fingerprint = '77:c4:03:8f:0c:ea:a4:de:7d:ce:41:d1:0a:b2:72:1d';
// データのあるパス
$remote_path = 'sftp://xxx.xxx.140.1:10022/path/';
// データを格納するパス
$local_path = './data/path/';
// SSH ユーザー名
$username = 'foo';
// SSH パスワード
$password = 'hogehoge';
// 取得するデータのファイル名
$filename = 'sample_data.dat';
$fp = fopen($local_path.$filename, 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_path.$filename);
curl_setopt($ch, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5, $md5_fingerprint);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_VERBOSE, true); // デバッグ用
$result = curl_exec($ch);
if ($result === false) {
echo "cURL Error: " . curl_error($ch);
} else {
echo "ファイルのダウンロードに成功しました。\n";
}
curl_close($ch);
fclose($fp);
ここでは適当なMD5フィンガープリントをセットしているのでエラーになるはず。出力は、* Trying 163.209.140.1:10022...
* Connected to xxx.xxx.140.1 (xxx.xxx.140.1) port 10022 (#0)
* User: foo
* SSH MD5 fingerprint: 77c4038f0ceaa4de7dce41d10ab2721d
* Denied establishing ssh session: mismatch md5 fingerprint. Remote 77c4038f0ceaa4de7dce41d10ab2721d is not equal to 77:c4:03:8f:0c:ea:a4:de:7d:ce:41:d1:0a:b2:72:1d
* Closing connection 0
cURL Error: Denied establishing ssh session: mismatch md5 fingerprint. Remote 77c4038f0ceaa4de7dce41d10ab2721d is not equal to 77:c4:03:8f:0c:ea:a4:de:7d:ce:41:d1:0a:b2:72:1d
みたいな感じ。「接続しようと思ったけど、MD5フィンガープリントが違うから接続できなかったよ。リモートのフィンガープリントは'77c4038f0ceaa4de7dce41d10ab2721d'だけど?」と、親切に正しいMD5フィンガープリントを教えてくれる👏$md5_fingerprint = '77c4038f0ceaa4de7dce41d10ab2721d';
これで、目的のデータを取得できる。