0

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?

MultiDev
  • 10,389
  • 24
  • 81
  • 148

2 Answers2

1

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..

Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65
0

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:

Community
  • 1
  • 1
Chris Hutchinson
  • 9,082
  • 3
  • 27
  • 33
  • I have firebug... what do I look at when I create the ftp account in the browser?... It always used to work just using file_get_contents, but now that Ive changed hosts its not working anymore. – MultiDev May 27 '12 at 20:12
  • At the cPanel login page, look at the HTML within the
    tag. There are usually elements; you need to note the name attribute on these elements. When you do the HTTP POST using cURL you need to include all of these fields. Take a look at these examples: http://davidwalsh.name/execute-http-post-php-curl, http://stackoverflow.com/questions/4889529/http-post-in-php-without-curl-using-username-password-param1-param2string-ar
    – Chris Hutchinson May 27 '12 at 23:48