It seems that ssh_tunnel is not working like this, ssh_tunnel will ask for "$ connection" to run the tunnel with the specified ip / port.
In your code, I assume that you are trying to create a local port that listens for cURL requests as a proxy, but basically you ask the ssh server to connect to itself (127.0.0.1).
As you can see in my example, the tunnel in Google is just a stream through which you can send streaming requests
$connection = ssh2_connect($sshServerIP, 22); ssh2_auth_password($connection, $sshServerUser, $sshServerPass); $sock = ssh2_tunnel($connection, "google.com", 80); fwrite($sock, "GET /? HTTP/1.0\r\n\r\n"); $body = ""; while (!feof($sock)) $body .= fgets($sock); echo $body;
You can open the local port before requesting a tunnel for your ip / port via:
$localsock = socket_create_listen(0); socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort);
And then your code with these changes:
$connection = ssh2_connect("64.246.126.25", 22); if(ssh2_auth_password($connection, "ubnt", "ubnt")) { if ($tunnel = ssh2_tunnel($connection, $YourInternetIPaccessible, $openedPort)) { $url = "http://checkip.dyndns.com/"; $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'; $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:$openedPort"); curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); $result = curl_exec($ch); echo $result; }else{ echo "Tunnel creation failed.\n"; } } else { echo "failed!"; }