ssh2_connect() give PHP Fatal error: Uncaught Error: Call to undefined function

20,929

After struggling for 2 days, My Linux Administrator suggested to install this package below.

sudo apt install php-ssh2

After installing the above package my ssh2_connect undefined function vanished.

Share:
20,929

Related videos on Youtube

sanainfotech
Author by

sanainfotech

self employed web designer and web developer.

Updated on September 18, 2022

Comments

  • sanainfotech
    sanainfotech over 1 year

    I am using

    • php7
    • Ubuntu 16.04.1 LTS

    I am trying to make a ssh2_connect connection using ssh2_auth_password, the reason for this is to get files from another server

    I am getting error as below PHP Fatal error: Uncaught Error: Call to undefined function ssh2_connect()

    $connection = ssh2_connect($connectionIp, 22, array('hostkey'=>'ssh-rsa'));
    
    if (ssh2_auth_password($connection, 'username', 'password')) {
            echo "Authentication Successful!\n";
          } else {
            die('Authentication Failed...');
          }
    

    I have install as below:

    sudo apt-get install libssh2-php && sudo /etc/init.d/apache2 restart
    

    I get the message

    Package libssh2-php is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    php-ssh2:i386 php-ssh2
    
    E: Package 'libssh2-php' has no installation candidate
    

    I used to use the same code for

    • php5
    • ubuntu 14.04

    This above code worked perfectly when I am upgraded to PHP and ubuntu higher version this code doesn't work.

    Thanks in advance for all you suggestion.

  • Todd Baur
    Todd Baur about 7 years
    Did you see in the message that you got suggested doing exactly this "However the following packages replace it: php-ssh2:i386 php-ssh2"? The answer hiding in plain sight :) I'm guessing that libssh2-php is either the PHP 5 / Ubuntu 14 library and php-ssh2 is the PHP 7 / Ubuntu 16 library.