4

我遵循了这个 Stack Overflow question thread的建议,但我一直遇到障碍。

我收到以下错误消息:不支持的协议:sftp

这是我的代码:

$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);

我使用 curl_version() 函数查看了我的 curl 信息,发现 sftp 似乎不在支持的协议数组中:

[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
        )

这是我的 cURL 版本过时的问题,还是根本不支持 SFTP 协议?

任何意见是极大的赞赏。

4

2 回答 2

3

也许尝试使用phpseclib,一个纯 PHP SFTP 实现。例如。

<?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');
?>
于 2012-06-27T03:11:31.013 回答
1

在撰写本文时,如果您可以访问 php,则不再需要依赖和使用 curl 的 sftp 实现。转到 php.net 并查看 ssh2 支持。在那里,您将找到如何以编程方式部署 sftp。

仍然在 php 中的另一种方法是使用在 phpseclib.sourceforge.net 中找到的 Net_SFTP 类/包。

该文档位于 http://phpseclib.sourceforge.net/documentation/net.html#net_sftp

预计?

万一你无法访问 php 或者你不想使用 php,我建议你仍然可以通过使用 expect 来停止担心 curl 的 sftp 实现。如果您的系统上还没有 expect,请转到 www.nist.gov/el/msid/expect.cfm 以获取它。一旦你得到它并安装它,那么脚本 sftp 将看起来类似于

#!/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.

于 2012-09-14T13:47:28.913 回答