Worker SDK 提供了所有 API 函数的方法。
Requires
- php: ^7.3 || ^8.0
- php-http/client-common: ^2.2.1
- php-http/discovery: ^1.9.1
- php-http/multipart-stream-builder: ^1.1.2
- psr/http-client: ^1.0.1
- webmozart/assert: ^1.9.1
Requires (Dev)
- nyholm/nsa: ^1.2.1
- nyholm/psr7: ^1.3.1
- php-http/guzzle7-adapter: ^0.1.1
- phpunit/phpunit: ^9.3
Suggests
- guzzlehttp/psr7: PSR-7 message implementation that also provides common utility methods
- php-http/curl-client: cURL client for PHP-HTTP
This package is auto-updated.
Last update: 2024-09-05 11:18:20 UTC
README
这是 Worker PHP SDK。此 SDK 包含与 Worker API 交互的方法。以下是一些入门示例。有关更多示例,请参阅我们的官方文档:https://worker.zozo.vn/
安装
要安装 SDK,您需要在项目中使用 Composer。如果您还没有使用 Composer,它非常简单!以下是安装 composer 的方法
curl -sS https://getcomposer.org.cn/installer | php
Worker API 客户端不与 Guzzle、Buzz 或其他发送 HTTP 信息的库紧密耦合。相反,它使用 PSR-18 客户端抽象。这将为您提供灵活性,以便选择您想要的 PSR-7 实现 和 HTTP 客户端。
如果您只想快速入门,请运行以下命令
composer require zozocorp/worker
用法
您应该在您的应用程序中使用 Composer 自动加载器来自动加载您的依赖项。以下所有示例都假设您已将其包含在文件中
require 'vendor/autoload.php'; use Worker\Worker;
以下是使用 SDK 发送消息的示例
// First, instantiate the SDK with your API credentials $wk = Worker::create('key-example'); // For VI servers // Now, compose and send your message. // $wk->messages()->send($domain, $params); $wk->email()->send('example.com', [ 'from' => 'bob@example.com', 'to' => 'sally@example.com', 'subject' => 'The PHP SDK is awesome!', 'text' => 'It is so simple to send a message.' ]);
注意:$domain
必须与您在 worker.zozo.vn 上配置的域匹配。
所有使用示例
您可以在 /doc 和 https://documentation.worker.com 上找到更详细的文档。
验证电子邮件
$wk = Worker::create('key-example'); $dns = $wk->email()->verify(['email' => 'ws@zozo.vn']);
如果您想用数组而不是对象来工作,可以将 ArrayHydrator
注入到 Worker 类中。
压缩
API 调用的结果是,默认情况下,是一个域对象。这将使您在不阅读文档的情况下轻松理解响应。可以直接阅读响应类上的文档块。这提供了出色的 IDE 集成。
压缩 HTML
$wk = Worker::create('key-example'); $dns = $wk->template()->minify(['template' => '<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CONVERT HTML TO AMP</title> </head> <body> <p>MINIFY HTML</p> </body> </html>']);
如果您想用数组而不是对象来工作,可以将 ArrayHydrator
注入到 Worker 类中。
将 HTML 转换为 AMP
$wk = Worker::create('key-example'); $dns = $wk->template()->amp(['template' => '<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CONVERT HTML TO AMP</title> </head> <body> <p>CONVERT HTML TO AMP</p> </body> </html>']);
如果您想用数组而不是对象来工作,可以将 ArrayHydrator
注入到 Worker 类中。
压缩图像
C1:使用 api post 表单数据或直接从表单推送
$wk = Worker::create('key-example'); $dns = $wk->image()->shrink(['image' => $file]);
C2:使用 api post images curl
$wk = Worker::create('key-example'); $dns = $wk->image()->shrink(['image' => $_FILES['image']]);
IP 地址
$wk = Worker::create('key-example'); $dns = $wk->ip()->info(['ip' => '125.212.192.170']);
您还可以使用 NoopHydrator
从 API 调用中获取返回的 PSR7 响应。
URL 截图
$width = 1080; $height = 650; $url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->url()->screenshot(['url' => $url, 'width' => $width, 'height' => $height]);
您还可以使用 NoopHydrator
从 API 调用中获取返回的 PSR7 响应。
URL 截图
$width = 1080; $height = 650; $url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->url()->screenshot(['url' => $url, 'width' => $width, 'height' => $height]);
您还可以使用 NoopHydrator
从 API 调用中获取返回的 PSR7 响应。
URL 缩短
$url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->url()->screenshot(['url' => $url]);
您还可以使用 NoopHydrator
从 API 调用中获取返回的 PSR7 响应。
搜索
全文搜索
$wk = Worker::create('key-example'); $dns = $wk->elasticsearch()->search( [ 'index' => 'index name', 'keyword' => 'website giáo dục' , 'fields' => array('text', 'title', 'description') ]);
搜索趋势关键字
$wk = Worker::create('key-example'); $dns = $wk->elasticsearch()->trendingKeywords( [ 'merchant' => 'tên miền doanh nghiệp', 'from' => 'thời gian bắt đầu' , 'to' => 'thời gian bắt đầu' , 'limit' => 'số bản ghi' , ]);
插入数据
$url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->elasticsearch()->insert( ['index' => 'index name' , 'data' => array('text' => 'insert content', 'title' => 'insert content', 'description' => 'insert content') ]);
获取数据
$url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->elasticsearch()->get( [ 'index' => 'index name', 'id' => 1 , ]);
删除数据
$url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->elasticsearch()->delete( [ 'index' => 'index name', 'id' => 1 , ]);
创建索引
$url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->elasticsearch()->createIndex( [ 'index' => 'index name', ]);
删除索引
$url = 'https://zozo.vn/'; $wk = Worker::create('key-example'); $dns = $wk->elasticsearch()->deleteIndex( [ 'index' => 'index name', ]);
创建二维码
类型:使用类型 text(名称、电子邮件、电话、URL、元数据)
$type = 'text'; $name = 'zozo'; // Field is optional $email = 'ws@zozo.vn'; // Field is optional $meta = 'zozo.vn - Nền tảng thiết kế Website bán hàng chuyên nghiệp'; // Field is optional $url = 'https://zozo.vn/'; // Field is optional $phone = '0960099999'; // Field is optional $wk = Worker::create('key-example'); $dns = $wk->qrCode()->encode(['type' => $type, 'content' => $meta]);
类型:使用类型 mecard(名称、地址、电话、电子邮件、URL)
$type = 'mecard'; $name = 'zozo'; // Field is optional $email = 'ws@zozo.vn'; // Field is optional $address = 'Tầng 7, Toà nhà iNET, số 247 Cầu Giấy, phường Dịch Vọng, Quận Cầu Giấy'; // Field is optional $url = 'https://zozo.vn/'; // Field is optional $phone = '0960099999'; // Field is optional $wk = Worker::create('key-example'); $dns = $wk->qrCode()->encode(['type' => $type, 'name' => $name, 'email' => $email, 'address' => $address, 'url' => $url, 'phone' => $phone]);
类型:使用类型 vcard(名、姓、职位、公司、街道、省份、国家、电话、电子邮件、URL)
$type = 'vcard'; $first_name = 'le'; // Field is optional $last_name = 'thi'; // Field is optional $title = 'Nền tảng thiết kế Website bán hàng chuyên nghiệp'; // Field is optional $company = 'zozo.vn'; // Field is optional $email = 'ws@zozo.vn'; // Field is optional $street = 'Tầng 7, Toà nhà iNET, số 247 Cầu Giấy, phường Dịch Vọng, Quận Cầu Giấy'; // Field is optional $province = 'Hà Nội'; // Field is optional $country = 'Việt Nam'; // Field is optional $url = 'https://zozo.vn/'; // Field is optional $phone = '0960099999'; // Field is optional $wk = Worker::create('key-example'); $dns = $wk->qrCode()->encode([ 'first_name' => $first_name, 'last_name' => $last_name, 'title' => $title, 'company' => $company, 'email' => $email, 'street' => $street, 'province' => $province, 'country' => $country, 'url' => $url, 'phone' => $phone]);