HowTo login with PHP (and Curl) into DirectAdmin
Hey guys, I am trying to create a script that will pull the log's from DirectAdmin so they can be parsed and submitted to a DB, the problem is the login of DirectAdmin.
I tried lots of script but I cant seem to get it to work...
Current script:
开发者_如何学Python$url = 'http://213.247.000.000:2222/CMD_SHOW_LOG?domain=mydomain.com&type=log';
$username = 'xxx';
$password = 'xxx';
$fields = array(
'username'=>urlencode($username),
'password'=>urlencode($password)
);
$fields_string='';
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($ch);
if($result===false) {
echo 'CURL ERROR: '.curl_error($ch);
}
curl_close($ch);
var_dump($result);
I've got it!
Made a little function out of it:
function getlog($ip,$username,$password,$domain) {
$url = 'http://'.$ip.':2222';
// set temp cookie
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
// make list of POST fields
$fields = array(
'referer' =>urlencode('/'),
'username'=>urlencode($username),
'password'=>urlencode($password)
);
$fields_string='';
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_URL,$url.'/CMD_LOGIN');
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($ch);
if($result===false) {
die('CURL ERROR: '.curl_error($ch));
} else {
curl_setopt($ch,CURLOPT_URL,$url.'/CMD_SHOW_LOG?domain='.$domain.'&type=log');
curl_setopt($ch,CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
if($result===false) {
die('CURL ERROR: '.curl_error($ch));
} else {
return $result;
}
}
}
You would probably better use the DirectAdmin PHP API class.
You need to use fiddler and firebug to figure out what is being sent when you login using your browser.
You then run your curl request through fiddler and make sure the exact same thing is posted.
You might need to set a refer, user-agent or cookies. You have too look at the request to tell what you need to send.
Web developer is a useful tool to disable cookies and javascript. See if you can login without cookies or javascript.
Use firebug will let you know if there are any ajax requests going on behind the scenes.
Blindly posting using curl will drive you insane.
You don't have to url-encode your username/password. Just pass the regular array to CURL, and it'll take care of the encoding.
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
精彩评论