I will produce!

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:cURLでSFTPを使う
update: 2025-07-15 09:15:24
アプリを設置するサーバーとアプリで使うデータのあるサーバーが違うときがある。データにHTTPでアクセスできれば簡単だったんだけど、SFTPでしかアクセスできないらしい。しかも、鍵認証ではなくてパスワード認証とか…😓
PHPのcurlでSFTPを使ってデータを取得する方法をまとめておく。

まずはcurlでSFTPをサポートしているかどうかを確認する。
<?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ファイルを実行する。(エラーになる😁)
<?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 に正しいフィンガープリントをセットすればOK👍

$md5_fingerprint = '77c4038f0ceaa4de7dce41d10ab2721d';
これで、目的のデータを取得できる。


good! 0 Buy me a coffee
GoogleMaps 料金体系の変更について
update: 2025-02-20 09:07:51
すでにあちこちの記事になっていますが、2025年3月から GoogleMaps の料金体系が変更されます。

https://developers.google.com/maps/billing-and-pricing/faq?hl=ja
https://internet.watch.impress.co.jp/docs/column/chizu3/1660301.html

弊社のAPIキーを利用して地図を作成しているお客さまについては、お客さまでAPIキーを設定していただいて順次地図表示のAPIキーを切り替えていく予定です。
お客さまごとに個別にご連絡いたしますので、よろしくお願いいたします。