青蛙/php-curl-sai

一个SAI(独立接口),用于启用curl请求的模拟。

1.1.2 2021-10-28 17:02 UTC

This package is auto-updated.

Last update: 2024-09-28 23:50:57 UTC


README

Latest Version on Packagist Build Status Total Downloads

这是一个从m-ender/php-SAI分叉的最小包,旨在使php项目中的curl请求的模拟更加简化。它经历的主要变化包括

  • 删除了不必要的代码文件,如多curl
  • 更新curl选项,因为该包最后一次更新是在2012年

安装

您可以通过composer安装此包

composer require frog/php-curl-sai:1.1.2

用法

在负责发出请求的类中

use FROG\PhpCurlSAI\SAI_Curl;
use FROG\PhpCurlSAI\SAI_CurlInterface;

 class SDKClass {
    // Define the variable to hold the cURL instance that will make a connection
    protected $cURL;

    /** Pass in the interface implemented by both the stub and actual class
     * to allow for detection of methods to be used by your IDE
    **/
    public function __construct(
        SAI_CurlInterface $cURL = null
    ) {
        // If no curl instance is provided, the one that makes the actual connection shall be used
        if ($cURL == null) $this->cURL = new SAI_Curl();
        else
            $this->cURL = $cURL;
    }

    public function get_data(
        string $access_token
    ){
        $auth_headers = [
            "Authorization: Bearer {$access_token}",
            "Content-Type: application/json",
        ];

        $options = [
            CURLOPT_URL => 'https://cool.url/data',
            CURLOPT_HTTPHEADER => $auth_headers,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
        ];

        // Setup a cURL handle via the interface
        $ch = $this->cURL->curl_init();

        // Add curl options via the interface
        $this->cURL->curl_setopt_array($ch, $options);
        // Execute the reqeust via the interface
        $response = $this->cURL->curl_exec($ch);

        if ($response === false) {
            // Retrieve the error via the interface
            $result = $this->cURL->curl_error($ch);
        } else {
            // Decode the result if need be
            $result = json_decode($response);
        }

        // Close the handle via the interface
        $this->cURL->curl_close($ch);

        return $result;
    }

    public function authorize(
        string $email,
        string $password,
    ){
        $request_body = [
            'email' => 'doe@mail.com',
            'password' => "12345678",
        ];

        $options = [
            CURLOPT_URL => 'https://cool.url/token',
            CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($request_body),
        ];

        // Setup a cURL handle via the interface
        $ch = $this->cURL->curl_init();

        // Add curl options via the interface
        $this->cURL->curl_setopt_array($ch, $options);
        // Execute the reqeust via the interface
        $response = $this->cURL->curl_exec($ch);

        if ($response === false) {
            // Retrieve the error via the interface
            $result = $this->cURL->curl_error($ch);
        } else {
            // Decode the result if need be
            $result = json_decode($response);
        }

        // Close the handle via the interface
        $this->cURL->curl_close($ch);

        return $result;
    }
 }

在需要执行模拟的类/文件中,例如在测试期间

use FROG\PhpCurlSAI\SAI_CurlStub;

public function stub_get_data() {
    // Create an instance of the stub class which implements SAI_CurlInterface
    $cURL = new SAI_CurlStub();
    $sdk = new SDKClass($cURL);

    $request_body = [
        'email' => 'doe@mail.com',
        'password' => "12345678",
    ];

    $options = [
        CURLOPT_URL => 'https://cool.url/token',
        CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
        CURLOPT_SSL_VERIFYPEER  => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode($request_body),
    ];

    // Set the response you want returned when the first cURL call is to be made
    $cURL->setResponse(
        '{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c","token_type":"Bearer","expires_in":3600}',
        // Pass in the cURL options (Needed to get the desired output)
        $options,
    );

    $token_result = $sdk->authorize(
      'doe@mail.com',
      "12345678"
    );

    $auth_headers = [
        "Authorization: Bearer {$token_result->access_token}",
        "Content-Type: application/json",
    ];

    $options = [
        CURLOPT_URL => 'https://cool.url/data',
        CURLOPT_HTTPHEADER => $auth_headers,
        CURLOPT_SSL_VERIFYPEER  => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
    ];

    // Set the response you want returned when the second cURL call is to be made
    $cURL->setResponse(
        '{"ref":"ac980b8e-afe5-49b8-b348-d0af00e2f556","date":"2021-02-21 22:20:32","code":"-11","MessageDescription":"MESSAGE REFERENCE LONGER THAN ALLOWED LENGTH"}',
        // Pass in the cURL options (Needed to get the desired output)
        $options,
    );

    $result = $sdk->get_data($token_result->access_token);

}

变更日志

请参阅CHANGELOG以获取最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全性

如果您发现任何与安全性相关的问题,请通过milleradulu@gmail.com发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。

PHP包模板

此包是使用PHP包模板生成的。