|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-05-05 09:20 UTC] bhaskarna at yahoo dot com
Description:
------------
CURLOPT_SSH_PUBLIC_KEYFILE and CURLOPT_SSH_PRIVATE_KEYFILE are not documented. They are useful when we require to scp or sftp a file thru curl api.
I could achieve successful sftp using above said curl_setopts
Test script:
---------------
<?php
$certPath="/home/y/conf/yapache/ssh-keys/akamai.id_rsa.pub";
$keyPath="/home/y/conf/yapache/ssh-keys/akamai.id_rsa";
error_reporting(E_ALL);
ini_set('display_errors', 1);
$ch = curl_init();
$localfile = 'scp-small-file.txt';
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'sftp://@72.246.194.54/21996/'.$localfile);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch,CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_SSH_PUBLIC_KEYFILE, $certPath);
curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, $keyPath);
curl_setopt($ch, CURLOPT_USERPWD,'sshacs');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
echo $error.' '.$error_no;
?>
* About to connect() to 72.246.194.54 port 22 (#0)
* Trying 72.246.194.54... * connected
* Connected to 72.246.194.54 (72.246.194.54) port 22 (#0)
* SSH authentication methods available: publickey
* Using ssh public key file /home/y/conf/yapache/ssh-keys/akamai.id_rsa.pub
* Using ssh private key file /home/y/conf/yapache/ssh-keys/akamai.id_rsa
* Initialized SSH public key authentication
* Authentication complete
* Connection #0 to host 72.246.194.54 left intact
* Closing connection #0
File uploaded succesfully. 0
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 00:00:02 2025 UTC |
also, when you change to CURLPROTO_SCP: $ diff -u b.php a.php --- b.php 2011-05-05 07:42:56.000000000 +0000 +++ a.php 2011-05-05 07:42:44.000000000 +0000 @@ -6,10 +6,10 @@ $ch = curl_init(); $localfile = 'scp-small-file.txt'; $fp = fopen($localfile, 'r'); - curl_setopt($ch, CURLOPT_URL, 'sftp://@72.246.194.54/21996/'.$localfile); + curl_setopt($ch, CURLOPT_URL, 'scp://@72.246.194.54/21996/'.$localfile); curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch,CURLOPT_VERBOSE, 1); - curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP); + curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SCP); curl_setopt($ch, CURLOPT_SSH_PUBLIC_KEYFILE, $certPath); curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, $keyPath); curl_setopt($ch, CURLOPT_USERPWD,'sshacs'); it doesn't work and throws the following error: * About to connect() to 72.246.194.54 port 22 (#0) * Trying 772.246.194.54... * connected * Connected to t72.246.194.54 (772.246.194.54) port 22 (#0) * SSH authentication methods available: publickey,keyboard-interactive * Using ssh public key file /home/y/conf/yapache/ssh-keys/akamai.id_rsa.pub * Using ssh private key file /home/y/conf/yapache/ssh-keys/akamai.id_rsa * Initialized SSH public key authentication * Authentication complete * SSH CONNECT phase done * scp: '/scp-small-file.txt': No such file or directory * Connection #0 to host 72.246.194.54 left intact * Closing connection #0 File upload error. 79