MultiDev
MultiDev

Reputation: 10649

Creating FTP account in cPanel using PHP's file_get_contents

I'm trying to create a new ftp account when a PHP form is processed. If I enter this address in my browser's address bar:

http://cpanel_username:cpanel_password@$mydomain.com:2082/json-api/cpanel?cpanel_jsonapi_version=2&cpanel_jsonapi_module=Ftp&cpanel_jsonapi_func=addftp&user=ftp_username&pass=ftp_password&homedir=/the/users/homefolder&quota=0

... json formatted results are shown on the screen and an ftp account is created as specified. However, when I use it in a PHP file, I can't get it to work! Here's what I have:

$user = "cpanel_username";
$pass = "cpanel_password";
$domain = "mydomain.com";
$fuser = "ftp_username";
$fpass = "ftp_password";
$fhomedir = "/the/users/homefolder";

$url = "http://$user:$pass@$domain:2082/json-api/cpanel?cpanel_jsonapi_version=2&cpanel_jsonapi_module=Ftp&cpanel_jsonapi_func=addftp&user=$fuser&pass=$fpass&homedir=$fhomedir&quota=0";

file_get_contents($url);

I also tried using CURL:

function new_get_file_contents($url) {
$ch = curl_init();
$timeout = 10; 
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch); 
curl_close($ch);
return $file_contents;
}

new_get_file_contents($url);

Any ideas why I can't get this to work?

Upvotes: 0

Views: 1946

Answers (2)

Abhishek Goel
Abhishek Goel

Reputation: 19771

Tried and Tested...

You need to enter variables

$cpaneluser = Your cpanel username
$cpanelpass = Your cpanel password
$domain = your domain name ( xyz.com )
$fuser = ftp username
$fpass = ftp password
$homedir = ftp directory 

$url = "http://$cpaneluser:$cpanelpass@$domain:2082/json-api/cpanel?";
$url .= "cpanel_jsonapi_version=2&cpanel_jsonapi_module=Ftp&cpanel_jsonapi_func=addftp&";
$url .= "user=$fuser&pass=$fpass&homedir=$fhomedir&quota=0";

var_dump($url);
$result = @file_get_contents($url);
if ($result === FALSE) 
die("ERROR: FTP Account not created. Please make sure you passed correct parameters.");
echo $result;

hope it helps..

Upvotes: 1

Chris Hutchinson
Chris Hutchinson

Reputation: 9212

It's likely not working because cURL doesn't have access to a cPanel session / cookies. Entering the URL works directly in a web browser, because the browser is preserving the cPanel session state between requests. The only way to solve this problem is to first login to cPanel using cURL and HTTP POST requests.

Tools like FireBug and Fiddler can help you see the HTML form elements or exact HTTP POST fields, including HTTP headers, sent when logging in. Emulating the login form process with cURL and then including your code should do the trick.

This may help you move along:

Upvotes: 0

Related Questions