cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

FTP w/ Explicit SSL/TLS example?

From: John Brahy <jb_at_popularllc.com>
Date: Tue, 8 Sep 2015 15:58:42 -0700

I've been trying to push to a remote ftp with explicit ssl server and the
connection keeps timing out.

Through filezilla I'm able to connect without a problem but not through
curl. I'm probably missing something and hoping someone on here would know
the best way to recreate the session using curl. Here's the debug output
from Filezilla and the sample code I've been working on in php/curl.

/*
Status: Resolving address of ftps.widgetsltd.com
Status: Connecting to 123.123.123.123:21...
Status: Connection established, waiting for welcome message...
Trace: CFtpControlSocket::OnReceive()
Response: 220-Microsoft FTP Service
Response: 220 Widgets, LTD FTP server
Trace: CFtpControlSocket::SendNextCommand()
Command: AUTH TLS
Trace: CFtpControlSocket::OnReceive()
Response: 234 AUTH command ok. Expecting TLS Negotiation.
Status: Initializing TLS...
Trace: CTlsSocket::Handshake()
Trace: CTlsSocket::ContinueHandshake()
Trace: CTlsSocket::OnSend()
Trace: CTlsSocket::OnRead()
Trace: CTlsSocket::ContinueHandshake()
Trace: CTlsSocket::OnRead()
Trace: CTlsSocket::ContinueHandshake()
Trace: TLS Handshake successful
Trace: Protocol: TLS1.0, Key exchange: RSA, Cipher:
AES-128-CBC, MAC: SHA1
Status: Verifying certificate...
Status: TLS connection established.
Trace: CFtpControlSocket::SendNextCommand()
Command: USER s-rokfri
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 331 Password required for s-rokfri.
Trace: CFtpControlSocket::SendNextCommand()
Command: PASS ********
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 230-This service and information contained therein belong
to Widgets, LTD.
Response: 230 User logged in.
Trace: CFtpControlSocket::SendNextCommand()
Command: OPTS UTF8 ON
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 200 OPTS UTF8 command successful - UTF8 encoding now ON.
Trace: CFtpControlSocket::SendNextCommand()
Command: PBSZ 0
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 200 PBSZ command successful.
Trace: CFtpControlSocket::SendNextCommand()
Command: PROT P
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 200 PROT command successful.
Status: Connected
Trace: CFtpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Trace: CFileZillaEnginePrivate::ResetOperation(0)
Trace: Measured latency of 141 ms
Status: Retrieving directory listing...
Trace: CFtpControlSocket::SendNextCommand()
Trace: CFtpControlSocket::ChangeDirSend()
Command: PWD
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 257 "/" is current directory.
Trace: CFtpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Trace: CFtpControlSocket::ParseSubcommandResult(0)
Trace: CFtpControlSocket::ListSubcommandResult()
Trace: state = 1
Trace: CFtpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: Directory listing of "/" successful
Trace: CFileZillaEnginePrivate::ResetOperation(0)
 */

Here's a code sample.

<?php

$ch = curl_init();

$ftp_data = array(
   'FIRST_NAME' => "John",
   'LAST_NAME' => "Doe",
   'STREET_ADDRESS' => "1313 Mockingbird Lane",
   'CITY' => "Atherton",
   'STATE' => "CA",
   'ZIPCODE' => "94027",
);

$ftp_data = join(",", $ftp_data);

$server_data = array(
   'transfer_id' => 123456789,
   'post_url' => "ftps://ftps.widgetsltd.com",
   'port' => 21,
   'username' => 'widgetsftp',
   'password' => 'password',
);

$filename = sprintf("%s-%s-%s.csv",
                    $server_data['transfer_id'],
                    microtime(TRUE),
                    rand(1000, 9999));

$temp_filename = sprintf("/tmp/%s", $filename);

$fp = fopen($temp_filename, 'w');
fputs($fp, sprintf("%s", $ftp_data));
fclose($fp);

$fp = fopen($temp_filename, 'r');
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

curl_setopt($ch, CURLOPT_URL, sprintf("%s/%s",
$server_data['post_url'], $filename));
curl_setopt($ch, CURLOPT_PORT, 21);

curl_setopt($ch, CURLOPT_USERPWD, sprintf("%s:%s",
$server_data['username'], $server_data['password']));

curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($temp_filename));

curl_setopt($ch, CURLOPT_USE_SSL, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_0);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'AES-128-CBC');

curl_setopt($ch, CURLOPT_FTP_SSL, CURLOPT_FTPSSLAUTH);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

print_r(array('curl_exec' => curl_exec($ch)));
print_r(array(
           'curl_errno' => curl_errno($ch),
           'curl_error' => curl_error($ch),
        ));

-- 
John Brahy
CTO/Partner
Popular Marketing
2101 East El Segundo Blvd
Suite 103
El Segundo, CA 90245
skype: jbrahy
844-TALK-DATA x202

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2015-09-09