literary.tr
New Member
Hello. With Sinusbot PHP Api How can I code a php panel?
It's really simple just follow these 5 steps:
Step 1: learn programming
Step 2: read the api docs
Step 3: write code
Step 4: ...
Step 5: profit!
It's really simple just follow these 5 steps:
Step 1: learn programming
Step 2: read the api docs
Step 3: write code
Step 4: ...
Step 5: profit!
I know a bit of programming, I But for the first time with api system is a trying coding. Do you or sinusbot.com have examples?
<?php
/* Array containing the information you would like to send to the API server. */
$data = Array (
'username' => 'the users account name',
'password' => 'the users password',
'botId' => 'the bot id you want to log in to'
);
/* Convert the array to a json string. */
$data_string = json_encode ($data);
/* Create a curl object, providing the API url. */
$ch = curl_init ('http://127.0.0.1/api/v1/bot/login');
/* Define we want to perform a POST request. */
curl_setopt ($ch, CURLOPT_POST, true);
/* Enter the data we want to send with the POST request (in our case, the json string we've generated). */
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data_string);
/* Set the returntransfer op true, basically saying we want to receive data. */
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
/* Create HTTP headers, containing the Content-Type (telling the server we're sending JSON data) and Content-Length (telling the server how long our data is). */
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array (
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
/* Execute the request, and save the data in $result. */
$result = curl_exec ($ch);
/* Show the results. */
print_r ($result);
?>
<?php
class Sinusbot {
var $token;
var $botId;
var $host;
var $api = "/api/v1/bot";
function __construct($sinushost, $bot) {
$this->host = $sinushost;
$this->botId = $bot;
$this->token = null;
}
function login($username, $password) {
$data = array(
'username' => $username,
'password' => $password,
'botId' => $this->botId
);
$data_string = json_encode($data);
$ch = curl_init($this->host.$this->api."/login");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if($result['success']) {
$token = $result['token'];
$this->token = $token;
$botId = $result['botId'];
$this->botid = $botId;
$result = array(
"success" => 1,
"message"=> null,
"data" => array(
"token"=> $token,
"botid"=> $botId
)
);
} else {
$result = array(
"success" => 0,
"message"=> "Exception",
"data" => null
);
}
return $result;
}
function disconnect($instanceId) {
$ch = curl_init($this->host.$this->api."/i/".$instanceId."/kill");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: bearer '.$this->token));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if($result['success']) {
$result = array(
"success" => 1,
"message"=> null
);
} else {
$result = array(
"success" => 0,
"message"=> "Exception"
);
}
return $result;
}
function connect($instanceId) {
$ch = curl_init($this->host.$this->api."/i/".$instanceId."/spawn");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: bearer '.$this->token));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if($result['success']) {
$result = array(
"success" => 1,
"message"=> null
);
} else {
$result = array(
"success" => 0,
"message"=> "Exception"
);
}
return $result;
}
function eventCall($instanceId, $event, $uid) {
$data = array(
'uid' => $uid
);
$data_string = json_encode($data);
$ch = curl_init($this->host.$this->api."/i/".$instanceId."/event/".$event);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: bearer '.$this->token));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if($result['success']) {
$result = array(
"success" => 1,
"message"=> null
);
} else {
$result = array(
"success" => 0,
"message"=> "Exception"
);
}
return $result;
}
}
?>