frog/php-curl-sai 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

frog/php-curl-sai

Composer 安装命令:

composer require frog/php-curl-sai

包简介

A SAI(Standalone interface) to enable stubbing of curl requests.

README 文档

README

Latest Version on Packagist Build Status Total Downloads

This is a minimal package forked from m-ender/php-SAI aimed at enabling a simplified approach to stubbing of curl requests in php projects. The main changes it underwent were:

  • Removal of unnecessary code files such as multi curl
  • Upgrading of curl options as the last update the package received was in 2012

Installation

You can install the package via composer:

composer require frog/php-curl-sai:1.1.2

Usage

In the class that's responsible for making the request:

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;
    }
 }

In the class/file that wants to perform a stub, e.g during tests:

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

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email milleradulu@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

PHP Package Boilerplate

This package was generated using the PHP Package Boilerplate.

统计信息

  • 总下载量: 458
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固