open-southeners/carbone-sdk

Carbone API SDK 对 Saloon v3 的非官方端口

1.2.0 2024-03-21 13:03 UTC

This package is auto-updated.

Last update: 2024-09-21 14:32:59 UTC


README

Carbone API SDK 对 Saloon v3 的非官方端口

入门

composer require open-southeners/carbone-sdk

Laravel 安装

为了让这个功能在 Laravel 应用中工作,你只需在你的 .env 文件的最底部添加以下内容

CARBONE_API_KEY='your-carbone-api-key'

要自定义 API 版本和其他设置,你可以在 config/services.php 中添加 carbone 数组项

<?php

return [

    // rest of services.php items here...

    'carbone' => [
        'key' => env('CARBONE_API_KEY', ''),
        'version' => '4',
    ],

];

使用方法

在 Laravel 中,它已被注入到你的应用程序容器中

$response = app('carbone')->template()->base64Upload($templateBase64);

if ($response->failed()) {
    throw new \Exception('Template upload failed!');
}

// This is extracted from official Carbone SDK: https://carbone.io/api-reference.html#upload-a-template-carbone-cloud-sdk-php
$templateId = $response->getTemplateId();

其他框架(或无框架)

如果你正在使用其他框架(或纯 PHP),你仍然可以将此作为独立库使用

$carbone = new Carbone('your-carbone-api-key');

// Use this if you want to use a different API version: https://carbone.io/api-reference.html#api-version
// $carbone = new Carbone('your-carbone-api-key', '4');

$templateBase64 = base64_encode(file_get_contents('path_to_your_template_here'));

$response = $carbone->template()->base64Upload($templateBase64);

if ($response->failed()) {
    throw new \Exception('Template upload failed!');
}

// This is extracted from official Carbone SDK: https://carbone.io/api-reference.html#upload-a-template-carbone-cloud-sdk-php
$templateId = $response->getTemplateId();

官方 SDK 与此之间的差异

  • 完全支持 Laravel(可选,因为它支持任何框架甚至原始 PHP)
  • 使用 Saloon v3 而不是 v1(这改善了所有类型,以更好地支持 IDE 自动补全等)
  • template()->upload() 方法替换为 template()->base64Upload(),以便可以使用上传方法发送 multipart/form-data POST 请求,而不是使用所有文件内容进行 base64 编码的 application/json
  • 添加了 template()->exists() 以获取模板是否存在(仅 HEAD 请求,无需获取任何内容)
  • 添加了 status()->fetch() 以获取状态(目前未公开文档,仅在 OpenAPI、Postman 等 此处可检查

你仍然可以在这里检查官方版本.

合作伙伴

skore logo

许可协议

此包是开源软件,根据 MIT 许可协议 许可。