API-连接安全吗? [英] API - is the connection secure?

查看:92
本文介绍了API-连接安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查与REST API的连接是否安全?

How to check if a connection to a REST API is secure?

1)我正在构建一个API,并希望在生成响应并将其返回给客户端之前确保连接正在运行SSL.在每个请求中都传递用户名和密码

1) I'm building an API and want to make sure that the connection is running SSL before the response is generated and returned to the client.. Username and password is passed in every request

2)以及如何使用SSL建立fsockopen连接?

2) And how can you make a fsockopen connection with SSL?

$this->url = 'api.domain.com';
$this->path = '/v1/json/account/user/pass/?id=123';

if($this->fp = fsockopen($this->url, $this->port, $errno, $errstr, $this->timeout)){
    $write = "$this->method $this->path HTTP/1.1\r\n";
    $write .= "Host: $this->url\r\n";
    $write .= "Content-Type: $this->content_type\r\n";
    $write .= "Connection: Close\r\n\r\n";

    fwrite($this->fp, $write);

    while($line = fgets($this->fp)){
        if($line !== false) $this->response .= $line;
    }

    fclose($this->fp);
}

推荐答案

只需检查 $ _ SERVER ['HTTPS'] .

位于 http://php.net/manual/zh/上的PHP手册reserved.variables.server.php 说:

"HTTPS"

'HTTPS'

如果脚本是通过HTTPS协议查询的,则设置为非空值.

Set to a non-empty value if the script was queried through the HTTPS protocol.

注意:请注意,将ISAPI与IIS一起使用时,如果未通过HTTPS协议发出请求,则该值将为关闭状态.

Note: Note that when using ISAPI with IIS, the value will be off if the request was not made > through the HTTPS protocol.

这篇关于API-连接安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆