PhP/Curl function for SSL API calls

kriak

Verified User
Joined
Jun 22, 2004
Messages
26
Hi !

I just wrote that little function to call the API using https protocol.

PHP:
function DA_SSL_Request($user, $password, $request, $method='GET', $post_content='', $host='localhost', $port=2222)
{
	$headers = '';
	$headers[] = "Content-Type: application/x-www-form-urlencoded";
	$headers[] = 'Content-length: '.strlen($post_content);
	$headers[] = 'Authorization: Basic '.base64_encode("$user:$password");
	
	$ch = curl_init();
	$complete_url = 'https://'.$host.':'.$port;
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method.' '.$request.' HTTP/1.1');
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_URL,$complete_url);
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 15); // Time out sec.
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
	
	curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
	curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
	
	$answer = curl_exec($ch);
	
	if( !$answer )
	{
		$answer['error'] = 1;
		$answer['error_msg'] = "CONNECTION ERROR ".curl_errno($ch).": ".curl_error($ch);
	}
	else
	{
		parse_str($answer,$answer);
	}
	
	return $answer;
}

And here is a POST example :

PHP:
$user = 'admin';
$password = 'pass';

$request = "/CMD_API_SHOW_USERS";
$method = 'POST'; // GET or POST
$post_content = "reseller=admin";

$host = 'locahost';
$port = 2222;

$retval = DA_SSL_Request($user, $password, $request, $method, $post_content, $host, $port);

// Display results.
echo "<pre>";
print_r($retval);
echo "</pre>";

And a GET example :

PHP:
$user = 'admin';
$password = 'pass';

$request = "/CMD_API_SHOW_USER_DOMAINS?user=admin";
$method = 'GET'; // GET or POST
$post_content = "";

$host = 'locahost';
$port = 2222;

$retval = DA_SSL_Request($user, $password, $request, $method, $post_content, $host, $port);

// Display results.
echo "<pre>";
print_r($retval);
echo "</pre>";

Thank you for your feedback. It can also be integrated into the API call class since it do not seem to support SSL.
 
Last edited:
If you want to use plain sockets, this works too:
PHP:
$socket = new HTTPSocket;
$socket->connect('ssl://yourserver',2222);

$socket->method('POST'); // this is an optional call; default is GET
$socket->query("/CMD_API_SHOW_USERS",array( 'user'  => 'admin' ));

if ($socket->fetch_parsed_body())
{
     print_r($socket->fetch_parsed_body());
}
;)
 
I tried the plain socket version with ssl:// and it just won't work for me.

P.S. I corrected my fonction, there was a little error... argument $post_content was written as $content in the fonction.
 
l0rdphi1 said:
If you want to use plain sockets, this works too:
PHP:
$socket = new HTTPSocket;
$socket->connect('ssl://yourserver',2222);

$socket->method('POST'); // this is an optional call; default is GET
$socket->query("/CMD_API_SHOW_USERS",array( 'user'  => 'admin' ));

if ($socket->fetch_parsed_body())
{
     print_r($socket->fetch_parsed_body());
}
;)

This isn't either working for me :s I'm using the Class but it doesn't work over HTTPS :s used ssl:// tsl:// nothing :(

Thx
 
Yes. You will need to recompile PHP with the " --with-openssl" option.

The logic being this: for PHP to communicate with DA over SSH, PHP has to know what SSH is, and OpenSSL does that for PHP's core :)

Phi1.
 
I would have tough that customapache has already this option on when it compile php.
 
Hi there,

I tried using your function with the CMD_API_POP:

PHP:
$user = 'blaat';
$password = 'blaat';

$request = "/CMD_API_POP";
$method = 'GET'; // GET or POST
$post_content = "action=create&domain=blaat.com&user=blaatblaat&passwd=blaat&quota=5";

$host = 'blaat.com';
$port = 2222;

$retval = DA_SSL_Request($user, $password, $request, $method,
$post_content, $host, $port);

// Display results.
echo "<pre>";
print_r($retval);
echo "</pre>";

but it keeps giving me:

Array
(
[error] => 1
[text] => Could not excute your request
[details] => You do not own that domain
)

Since I'm sure this user does own this account I think the multiple post_content content doesn't get passed through (this is also what John at DA Support told me). Any clues?

Regards,

Tristan
 
The bad thing about this function is that is requiring to supply also the password.

For the socket case there is no need to supply the password.

Is possible to use curl way without the password ?
 
Back
Top