Hi there,
i am trying to get a wrapper working around an API with Laravel. So i created a Model called Server. This is my Server Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Server extends Model
{
protected $remote_user = 'api';
protected $remote_pass = 'testtest';
public static function connect($method, $data)
{
$remote_url = 'https://xxx.xx.xxx.xx:8080/remote/json.php';
if(!is_array($data)) return false;
$json = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
if($data) curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
// needed for self-signed cert
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
// end of needed for self-signed cert
curl_setopt($curl, CURLOPT_URL, $remote_url . '?' . $method);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}
In my Controller i have this messy code
$remote_user = 'api';
$remote_pass = 'testtest';
$result = Server::connect('login', array('username' => $remote_user, 'password' => $remote_pass, 'client_login' => false));
$data = json_decode($result, true);
$session_id = $data['response'];
$reseller_id = 0; // this id has to be 0 if the client shall not be assigned to admin or if the client is a reseller
$params = array(
'company_name' => 'testcompany',
......
......
);
$result = Server::connect('client_add', array(
'session_id' => $session_id,
'reseller_id' => $reseller_id,
'params' => $params
));
$result = json_decode($result, true);
if($result) {
return $result;
}
So far its working, but i want to clean up this of course. I cant get this thing working to only call
$result = Server::connect('client_add', array(
'session_id' => $session_id,
'reseller_id' => $reseller_id,
'params' => $params
));
in my Controller. Can someone help me how i would pass 'login', array('username' => $remote_user, 'password' => $remote_pass, 'client_login' => false) to the method so i dont have to write this everytime?