file_get_contents不适用于SSH隧道

时间:2017-07-13 15:06:38

标签: php linux ssh tunnel

我创建了一个隧道(SSH),以便访问从我的本地PC阻止的URL。这非常有效,我可以从浏览器访问此网址http://localhost:8888,我将获得预期的内容。此URL也适用于我用于测试的REST客户端。但是,如果我从PHP调用它:

file_get_contents('http://localhost:8888') 

我每次都会收到此错误:failed to open stream: Connection refused

所以我的猜测是file_get_contents不能很好地与SSH隧道配合使用。是否可以使用替代功能来实现此目的?

1 个答案:

答案 0 :(得分:0)

我使用Subcomponent包解决了这个问题。

Ubuntu 16上的安装

php-ssh2

使用用户名和密码:

sudo apt-get install php-ssh2

使用id_rsa(在ssh-copy-id之后)

我建议使用此方法,因为我们可能不希望在源代码中粘贴普通密码。

$conn = ssh2_connect($ip, 22);
ssh2_auth_password($conn, $user,$pass);
ssh2_scp_recv($conn, '/remote_path', '/local_path');

其他信息

此软件包的旧名称:libssh2-php

文档:

  

http://php.net/manual/en/function.ssh2-scp-recv.php

已连接的问题:

  

How to SFTP with PHP?

是否还有使用的可能性:

$conn = ssh2_connect($ip, 22);
ssh2_auth_pubkey_file($conn, $user,
    "/home/{$user}/.ssh/id_rsa.pub",
    "/home/{$user}/.ssh/id_rsa", 'secret')
ssh2_scp_recv($conn, '/remote_file', '/local_file');

但我不知道如何将其与file_get_contents("ssh2.sftp://{$user}:{$pass}@{$ip}:22{$remote_path}")) 一起使用。

相关问题