jalendport / craft-fetch
在Craft模板中利用Guzzle HTTP客户端。
2.0.0-beta.1
2023-06-08 22:22 UTC
Requires
- craftcms/cms: ^4.0.0
This package is auto-updated.
Last update: 2024-09-09 00:58:35 UTC
README
Craft CMS 3.x的抓取插件
在Craft模板中利用Guzzle HTTP客户端。
需求
此插件需要Craft CMS 3.0.0或更高版本。
安装
要安装插件,请按照以下说明操作。
-
打开您的终端并转到您的Craft项目目录
cd /path/to/project
-
然后告诉Composer加载插件
composer require jalendport/craft-fetch
-
在控制面板中,转到设置 → 插件,然后点击Fetch的“安装”按钮。
使用Fetch
此插件旨在与标准的Guzzle请求选项一起工作
参数
示例用法
请求
{% set client = { base_uri : 'https://gtmetrix.com', timeout : 10 } %} {% set options = { auth : ['username', 'password'], form_params : { url : 'https://www.google.co.uk' } } %} {% set request = fetch(client, 'POST', 'api/0.1/test', options) %}
响应(成功)
{ "statusCode":200, "reason":"OK", "body": { "credits_left":30, "test_id":"JDHFbrt7", "poll_state_url":"https:\/\/gtmetrix.com\/api\/0.1\/test\/JDHFbrt7" } }
您可以通过添加一个额外的参数false
来获取字符串响应
{% set request = fetch(client, 'POST', 'api/0.1/test', options, false) %}
响应(错误)
{ "error":true, "reason":"Client error: `POST https:\/\/gtmetrix.com\/api\/0.1\/test` resulted in a `401 Authorization Required` response:\n{\u0022error\u0022:\u0022Invalid e-mail and\/or API key\u0022}\n\n" }
Fetch路线图
一些待办事项和潜在功能的想法
由Luke Youell提供