将ftp命令行cURL转换为PHP cURL

时间:2015-09-01 12:00:42

标签: php curl ftp

如何将以下cURL行转换为php cURL。

curl -v --cacert [linkToCertificate] -k --ftp-ssl -T "[fileToUpload]" -P - ftp://[user]:[password]@[URL]

到目前为止我尝试过:

curl_setopt($ch, CURLOPT_VERBOSE, TRUE); #-v
curl_setopt($ch, CURLOPT_PORT, '-');
curl_setopt($ch, CURLOPT_URL, $ftp_server . $source_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $ftp_user . ':' . $ftp_password);
curl_setopt($ch, CURLOPT_FTP_USE_EPRT, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 400);
curl_setopt($ch, CURLOPT_FILE, $file);

//SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CAINFO, $ftp_certificate);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLPROTO_FTP); 

1 个答案:

答案 0 :(得分:0)

我发现并回答了我的问题。 问题是curl在被动模式下运行ftp连接。我需要它以主动模式运行。

因此,我的解决方案是添加此选项:

curl_setopt($ch, CURLOPT_FTPPORT, 0);

完全卷曲解决方案:

curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_FTPPORT, 0);
curl_setopt($ch, CURLOPT_URL, $ftp_server . $destination_file);
curl_setopt($ch, CURLOPT_USERPWD, $ftp_user . ':' . $ftp_password);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 400);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($source_file));
curl_setopt($ch, CURLOPT_CAINFO, $ftp_certificate);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_ALL);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_SSL);