如何使用codeigniter连接到api



Restful API中的新手。如何连接到API?我试过几种方法来连接它,但似乎都不起作用。我提供了下面的restful API连接代码,但我不知道如何连接。我提供了以下代码。任何帮助都将不胜感激。非常感谢。

视图:

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

控制器:

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);

我使用以下代码,这些代码通过wordpress API和codeigniter 3进行了测试。您需要修改$opts数组。

$方法是post、get、put。。。

$url api url。。。

$opts=array('https'=>array('method'=>"GET"'header'=<授权:承载$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;
}

//以下是您的代码,但稍作修改以更改标题并建议删除授权。我还去除了一些不必要的卷曲。我更改了$data_array以包含头,但也尝试了它的原始形式,但内容类型为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);

相关内容

  • 没有找到相关文章

最新更新