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

查看:43
本文介绍了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'].

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

The PHP manual at http://php.net/manual/en/reserved.variables.server.php says:

'HTTPS'

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

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

注意:请注意,在 IIS 中使用 ISAPI 时,如果请求不是通过 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天全站免登陆