PDA

View Full Version : PhP/Curl fonction for SSL API calls


kriak
07-02-2004, 09:42 AM
Hi !

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


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 :


$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 :


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

FarCry
07-02-2004, 10:45 AM
good work

l0rdphi1
07-02-2004, 01:46 PM
If you want to use plain sockets, this works too:$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());
};)

kriak
07-09-2004, 07:00 AM
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
07-09-2004, 07:38 PM
You'll need to have your PHP complied with OpenSSL I think :)

fusionictnl
07-17-2004, 02:51 AM
Originally posted by l0rdphi1
If you want to use plain sockets, this works too:$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

l0rdphi1
07-17-2004, 11:13 PM
Hello,

Do you have OpenSSL compiled into PHP? :)

Phi1.

fusionictnl
07-18-2004, 10:30 AM
Da works over HTTPS, so does I have to recompile my php to enable it?

l0rdphi1
07-18-2004, 10:51 AM
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.

kriak
07-19-2004, 07:56 AM
I would have tough that customapache has already this option on when it compile php.

l0rdphi1
07-19-2004, 11:05 AM
Originally posted by kriak
I would have tough that customapache has already this option on when it compile php.
Me too :(

Phi1.

tristan
07-12-2006, 04:30 AM
Hi there,

I tried using your function with the CMD_API_POP:


$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