Reputation: 219
newbie here in Restful API. How can I connect to the API? I've tried several ways on how to connect it but nothing seems to work. I provided the restful API connection codes below but I don't know how to connect it. I have provided my codes below. Any help will be appreciated. Thank you.
Views:
<form action="<?=base_url('account/dologin');?>" method="post">
<!-- login -->
<form id="doLogin">
<div class="input-group mb-3 dropShadow" style="padding:35px 35px 5px 45px;">
<input type="text" class="signinrow" name="formUsername" placeholder="Username">
<div class="input-group-append">
<div class="input-group-text" style="background:transparent; border: none;">
<i class="fas fa-at"></i>
</div>
</div>
</div>
<div class="input-group mb-4 dropShadow" style="padding:1px 35px 25px 45px;">
<input type="password" class="signinrow" name="formPassword" placeholder="Password">
<div class="input-group-append">
<div class="input-group-text" style="background:transparent; border: none;">
<span class="fas fa-lock"></span>
</div>
</div>
</div>
<div class="text-bold text-sm afterLoginForm text-center" >
<p class=" mb-2" style="font-size: 18px;">
<a data-toggle="modal" data-target="#recoAcc" type="button" class="dropShadow bebas" ><u>I forgot my password </u></a><br>
</p>
</div>
<br>
<br>
<div>
<button id="btnSubmit"> Sign In</button>
<img id="submit_preloader" src="<?=pubPath('/assets/imgs/order_button_preloader.gif'); ?>" alt="Processing..." style=" display:none;" id="reg_preloader2" >
</div>
</form>
Controller:
public function dologin()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
$ci =& get_instance();
$username = $ci->input->post('formUsername');
$password= $ci->input->post('formPassword');
$ipAddress =$ip;
$baseSessionID = $ip.$username;
$url = "https://blabla.com/Auth/login";
$data_array = array('username' => $username,'password' => $password,'baseSessionID' => $baseSessionID,'ipAddress' => $ipAddress);
$data = http_build_query($data_array);
$data1 = array(
'Content-Type: application/x-www-form-urlencoded',
'Auth-Key: simplerestapi',
'Client-Service: frontend-client'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data1);
$reply=curl_exec($ch);
$decode = json_decode($reply,true);
$username1=$decode['message'];
$password1=$decode['password'];
$userfailed=$decode['mali'];
$_SESSION["username"] = $this->input->post("formUsername");
if($username1 ==='Successfully login.'||$password1 ==='Successfully login.'){ //Login success
redirect(site_url('account/dashboard'), $data);
}elseif($userfailed ==='failed username'){ //Wrong Username
echo '<script>alert("Wrong Password or Username!");</script>';
redirect(site_url(), $data);
curl_close($ch);
Upvotes: 1
Views: 973
Reputation: 111
I use the following code which is tested with the wordpress API and codeigniter 3. You'll need to amend the $opts array.
$method would be post, get, put ...
$url the api url ...
$opts = array('https'=>array('method'=>"GET",'header'=>"Authorization: Bearer $token"));
public function CallAPI($method, $url, $opts = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($opts)
curl_setopt($curl, CURLOPT_POSTFIELDS, $opts);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($opts)
$url = sprintf("%s?%s", $url, http_build_query($opts));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
// The following is your code but slightly amended to change the header and suggest Authorization which is commented out. I also removed some unnecessary curl. I changed your $data_array to include the header but try it in its original form as well but with content-Type as application/json.
$url = "https://blabla.com/Auth/login";
$data_array = array('username' => $username,'password' =>
$password,'baseSessionID' => $baseSessionID,'ipAddress' => $ipAddress,
'https' => array('header' => 'Content-Type: application/json','Auth-Key:
simplerestapi', 'Client-Service: frontend-client'));
$data = http_build_query($data_array);
/*
$data1 = array(
//'Content-Type: application/x-www-form-urlencoded',
'Content-Type: application/json',
'Auth-Key: simplerestapi',
//'Authorization: Basic '. base64_encode("app_key:app_secret")
'Client-Service: frontend-client'
);
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $data1);
$reply=curl_exec($ch);
if($e = curl_error($ch)){
echo $e;
}else{
echo $decode = json_decode($reply,true);
}
curl_close($ch);
Upvotes: 1