lukeyouell / 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 not auto-updated.
Last update: 2024-07-12 12:29:25 UTC
README
Craft CMS 3.x的Fetch插件
在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 提供