Reputation: 3772
I followed the advice from this Stack Overflow question thread, but I keep hitting a snag.
I am receiving the following error message: Unsupported protocol: sftp
Here is my code:
$ch = curl_init();
if(!$ch)
{
$error = curl_error($ch);
die("cURL session could not be initiated. ERROR: $error."");
}
$fp = fopen($docname, 'r');
if(!$fp)
{
$error = curl_error($ch);
die("$docname could not be read.");
}
curl_setopt($ch, CURLOPT_URL, "sftp://$user_name:$user_pass@$server:22/$docname");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($docname));
//this is where I get the failure
$exec = curl_exec ($ch);
if(!$exec)
{
$error = curl_error($ch);
die("File $docname could not be uploaded. ERROR: $error.");
}
curl_close ($ch);
I used the curl_version() function to see my curl information, and found that sftp doesn't seem to be in the array of supported protocols:
[version_number] => 462597
[age] => 2
[features] => 1597
[ssl_version_number] => 0
[version] => 7.15.5
[host] => x86_64-redhat-linux-gnu
[ssl_version] => OpenSSL/0.9.8b
[libz_version] => 1.2.3
[protocols] => Array
(
[0] => tftp
[1] => ftp
[2] => telnet
[3] => dict
[4] => ldap
[5] => http
[6] => file
[7] => https
[8] => ftps
)
Is this a matter of my version of cURL being outdated, or is the SFTP protocol simply not supported at all?
Any advice is greatly appreciated.
Upvotes: 5
Views: 4549
Reputation: 11
As of this writing, there is no longer a need to depend on and use curl's sftp implementation if you have access to php. Goto php.net and look at ssh2 support. There you will find an how to deploy sftp programmatically.
An alternative, still in php, is to use the Net_SFTP class/package to found at phpseclib.sourceforge.net.
The documentation is at http://phpseclib.sourceforge.net/documentation/net.html#net_sftp.
Expect?
In the unlikely event that you don't have access to php or you don't want to use php, I suggest that you can still stop worrying about curl's sftp implementation by using expect. If you do not already have expect on your system, goto www.nist.gov/el/msid/expect.cfm to get it. Once you get it and install it, then to scripting sftp will look something similar to
#!/usr/bin/expect
spawn /usr/bin/sftp <user@hostname>
expect "password:"
send "<mypassword>\r"
expect "sftp> "
send "get <remotefile> \r"
expect "sftp> "
send "bye \r"
exit 0
where you will replace with your own values.
The idea is to stop wasting time on trying to use sftp with curl. You can use php's implementation or you can script sftp using expect. Hope this helps and saves someone from wasting time.
Upvotes: 1
Reputation: 16812
Maybe try using phpseclib, a pure PHP SFTP implementation. eg.
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');
?>
Upvotes: 3