欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php Authorization Basic 认证 curl 有大用 有大大用

php实现接口http协议中的Authorization Basic认证、调用

      

我们应用API接口实现中通过Authorization Basic认证是比较常见的,下面谈谈使用php实现接口认证、调用的方法。

需求场景描述

应用系统API接口,需要通过Authorization Basic认证实现,接口方给客户端相应的密钥才能实现认证,并且客户端也通过Authorization Basic认证的调用来实现通信。

实现方法是,将http协议请求头中压入认证字符串,认证字符串可以以base64编码加密,格式如:Authorization:Basic base64_encode("$name:$pwd")

接口实现Authorization Basic认证

php代码如下:


<?php
//一个验证用户的接口范例
public function validUser(){
   
header("Content-Type: text/html; CharSet=UTF-8");
   
$requestHeaders = apache_request_headers();
   
if (array_key_exists('Authorization', $requestHeaders)) {
       
list($username, $password) = explode(':', base64_decode(explode(' ', $requestHeaders['Authorization'])[1]));
//记录接口日志
       
$log_content=date("Y-m-d H:i:s ", time())."Authorization username,password=".$username.';'.$password;
       file_put_prepend (
$log_content, $this->log_file);
//$auth_key=base64_decode(explode(' ', $requestHeaders['Authorization'])[1]);
       
$pwd_en  = md5(md5($password));
//$auth_key=$_POST['auth_key'];
       
通过数据库或其他方式认证取得用户信息$userInfo
if (!empty($userInfo)) {
   
$this->responCode(200);
   
$this->uname=$username;
//echo "登录成功";
} else  {
   
$this->responCode(403); exit;
}
  }
else {
       
$this->responCode(401); exit;
   }
}
private function responCode($code){
   
if (401 === $code) {
       
header("401 Unauthorized");
       
header("WWW-Authenticate: Basic");
   }
elseif (403 === $code) {
       
header("403 Forbidden");
   }
   
else {
       
header('200 OK');
   }
}
?>

调用Authorization Basic认证API接口

<?php
public function
callValidUser(){
   
$url=$base_url.'/validuser';
   
$name='test';  $pwd='1234';
   
$headers =array('Authorization:Basic '.base64_encode("$name:$pwd") );
   
$ch = curl_init();
   
$postData=array('username'=>$_POST['username']);
   
curl_setopt($ch, CURLOPT_HEADER, 0);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   
curl_setopt($ch, CURLOPT_POST, 1);  curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
   
//curl_setopt($ch, CURLOPT_HTTPGET, true);   curl_setopt($ch, CURLINFO_HEADER_OUT, true);
   
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);//跳过https验证
   
curl_setopt($ch, CURLOPT_URL, $url);
   
$response = curl_exec($ch);
// echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
   
curl_close($ch);
   
echo $response;
}
?>

 

来自  https://blog.csdn.net/yan_dk/article/details/110141971




php实现http协议中的Authorization Basic认证


HTTP协议中的 身份认证

image.png



客户端发起请求,未携带Authorization头,服务端返回 401 Unauthorized及响应头 WWW-Authenticate: Basic告诉客户端以Basic方式进行身份验证,客户端会显示输入框,需要输入帐号密码,发起请求会携带Authorization: Basic base64_encode(username:password),服务端进行鉴权。

image.png

php代码实现如下

<?php
header("Content-Type: text/html; CharSet=UTF-8");
$requestHeaders = apache_request_headers();

if (array_key_exists('Authorization', $requestHeaders)) {
   
list($user, $pass) = explode(':', base64_decode(explode(' ', $requestHeaders['Authorization'])[1]));
   
if ($user === 'admin' && $pass === '123456') {
       responCode(
200);
       
echo "登录成功";
   }
else  {
       responCode(
403);
   }
}
else {
   responCode(
401);
}


function responCode($code)
{
   
if (401 === $code) {
       
header("401 Unauthorized");
       
header("WWW-Authenticate: Basic");
   }
elseif (403 === $code) {
       
header("403 Forbidden");
   }
   
else {
       
header('200 OK');
   }
}
?>


来自  https://blog.csdn.net/weixin_43197795/article/details/108338900



普通分类: