【问题标题】:Private API Creation Standards - Is this API correct?私有 API 创建标准 - 这个 API 是否正确?
【发布时间】:2023-03-08 00:24:01
【问题描述】:

所以我们想创建一个私有 API,允许其他人与我们的服务进行交互,例如通过 api 将某些内容保存在我们的数据库中或获取某些信息。

API 本身只需要可以通过任何编程语言传递给 API 的 JSON 数据。与 facebook 等其他 API 不同,我们不会向用户提供任何文件以供用户使用 API,例如 facebook 确实提供了不同语言的类以与他们的 api 交互。所以我们认为 api 用户可以使用任何语言向我们发送 json 数据,并且基于该 json 数据,我们可以通过他们通过查询字符串提供的use 参数调用 api 的某些函数。

以下是最基本的 API 代码,可让您了解其工作原理:

class Api
{
    /**
     * Default function called when API url is accessed
     *
     * @return mixed
     * @throws Exception
     */
    public function index()
    {
        $appId = isset($_GET['appId']) ? $_GET['appId'] : null;
        $apiKey = $_GET['apiKey'] ? $_GET['apiKey'] : null;
        $method = $_GET['use'] ? $_GET['use'] : null;

        $data = json_decode(file_get_contents("php://input"), true);

        if ($this->validateRequest($appId, $apiKey, $method)) {
            return call_user_func_array(array($this, $method), array($data));
        }
    }

    private function validateRequest($appId, $apiKey, $method)
    {
        # check referer
        $requestUri = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : null;

        if (!$requestUri) {
            $this->bye('HTTP_REFERER is required!');
        }

        # check if method exists
        if (!$method || !method_exists('Apicall', $method)) {
            $this->bye('Invalid \'use\' value!');
        }

        # check appId, apiKey and requestUri
        if (!$appId) {
            $this->bye('Invalid appId!');
        }

        if (!$apiKey) {
            $this->bye('Invalid apiKey!');
        }

        $db = new DB();
        $db->where('appid', $appId);
        $db->where('apikey', $apiKey);
        $db->where('request_uri', pathWithoutEndingSlash($requestUri));
        $db->get();

        if ($db->exists() && $db->result_count() > 0) {
            return true;
        }

        $this->bye('Request Denied!');
    }

    private function bye($error)
    {
        echo json_encode(array('Error' => $error));
        exit;
    }

    private function getIntervals()
    {
        $values = array('monthly', 'quarterly', 'yearly', 'single_payment');
        echo json_encode($values);
    }
}

可以看出,我们验证提供的appIdapiKeyrequestURI是否存在于我们的数据库中(我们已经为api用户生成)然后调用通过use查询字符串变量指定的方法电话:

http://localhost/myapi/api.php?appId=local&apiKey=98a4d2e5b7fa21d424a0932c7f47a6943d57d64b&use=getIntervals

这是制作私有 API 的正确方法还是我仍然遗漏了什么?

【问题讨论】:

    标签: php api standards web-standards


    【解决方案1】:

    任何监听请求的人(因此,如果您在 WIFI 上调用您的 api 或作为 AJAX 调用)将能够看到您的 appId 和 apiKey。所以他们也可以调用你的api。更好的方法是:

    • a) 在 API 服务器上使用 SSL 来加密调用(从非 SSL POST 到 SSL 已确保加密)
    • b) 使用 POST 或标头发送您的身份验证数据,而不是将其添加到 URL 中
    • c) 让客户端登录一次并为当前会话提供一个令牌,该令牌允许在 x 分钟内连接,或者在最后一次通话的 x 分钟后丢弃
    • d) 将令牌(从 c)锁定到客户端 IP,如果另一个 IP 连接到相同的令牌,则使令牌无效

    如果你想要一个快速的 REST API,我可以推荐 Restler。它还包含身份验证。 http://luracast.com/products/restler

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2017-06-25
      • 1970-01-01
      • 2016-12-17
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多