leowebguy / simple-guzzle
为 Craft CMS 设计的简单 Guzzle 插件
2.0.0
2024-03-27 02:38 UTC
Requires
- php: ^8.2
- craftcms/cms: ^5.0
Requires (Dev)
- craftcms/ecs: dev-main
- craftcms/phpstan: dev-main
- craftcms/rector: dev-main
- laravel/pint: ^1.14.0
README
一种简单调用 guzzle 并从 API 缓存数据的途径
目录
安装
composer require leowebguy/simple-guzzle && php craft plugin/install simple-guzzle
参数
使用方法
获取 Json
{% set request = guzzle({
base_uri : 'https://official-joke-api.appspot.com/'
}, 'GET', 'random_joke') %}
{% header "Content-Type: application/json; charset=utf-8" %}
{{ request|json_encode|raw }}
结果
{
"type": "general",
"setup": "Who is the coolest Doctor in the hospital?",
"punchline": "The hip Doctor!",
"id": 302
}
获取非 Json
如果结果不能被解析为 Json,插件将自动返回字符串
{% set request = guzzle({
base_uri : 'http://api.geonames.org/'
}, 'GET', 'srtm1?lat=50.01&lng=10.2&username=demo&style=full') %}
{{ request }}
结果
"208"
带有选项的 GET
{% set request = guzzle({
base_uri: 'https://currency-converter5.p.rapidapi.com/'
}, 'GET', 'currency/convert?from=USD&to=CAD&amount=1', {
headers: {
'X-RapidAPI-Key': 'you-api-key',
'X-RapidAPI-Host': 'currency-converter5.p.rapidapi.com'
}
}) %}
结果
{
"base_currency_code": "USD",
"base_currency_name": "United States dollar",
"amount": "1.0000",
"updated_date": "2023-04-19",
"rates": {
"CAD": {
"currency_name": "Canadian dollar",
"rate": "1.3443",
"rate_for_amount": "1.3443"
}
},
"status": "success"
}
带有缓存的 GET
默认情况下,缓存将设置为 0,表示不缓存,除非您提供以秒为单位的缓存持续时间。例如:3600 = 1小时,86400 = 24小时
{% set request = guzzle({
base_uri : 'https://official-joke-api.appspot.com/'
}, 'GET', 'random_joke', {}, 3600) %}
{% header "Content-Type: application/json; charset=utf-8" %}
{{ request|json_encode|raw }}
使用 Bearer 认证的 POST
{% set request = guzzle({
base_uri: 'https://api-ssl.bitly.com/'
}, 'POST', 'v4/bitlinks', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer my-bearer-token'
},
body: '{
"domain": "bit.ly",
"long_url": "https://craftcms.com/"
}'
}) %}
结果
{
"created_at": "2023-04-20T00:10:44+0000",
"id": "bitly.is/41no4QW",
"link": "https://bitly.is/41no4QW",
"custom_bitlinks": [],
"long_url": "https://craftcms.com/",
"archived": false,
"tags": [],
"deeplinks": [],
"references": {
"group": "https://api-ssl.bitly.com/v4/groups/Bi331psZCY8"
}
}
使用基本认证的 POST
{% set request = guzzle({
base_uri: 'https://gtmetrix.com/api/2.0/'
}, 'POST', 'tests', {
headers: {
'Content-Type': 'application/vnd.api+json'
},
auth: ['my-auth', ''],
body: '{
"data": {
"type": "test",
"attributes": {
"url": "https://craftcms.com"
}
}
}'
}) %}
基本认证通常接受用户名和密码作为参数,例如:上面示例中的 auth: ['username', 'password'],,其中 token 作为用户名传递,密码为空,根据 gtmetrix 文档。
结果
{
"data": {
"id": "tMsUIR0M",
"type": "test",
"attributes": {
...
}
},
"meta": {
"credits_left": 49,
"credits_used": 1
},
"links": {
"self": "https://gtmetrix.com/api/2.0/tests/tMsUIR0M"
}
}
Twig 输出
确保大家理解如何将结果输出到模板中。
{% set request = guzzle({
base_uri: 'https://api.openai.com/v1/'
}, 'POST', 'completions', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer my-bearer-token'
},
body: '{
"model": "text-davinci-003",
"prompt": "Hello, who are you?"
}'
}) %}
Question: Hello, who are you?
Answer: {{ request.choices[0].text }}
输出
Question: Hello, who are you? Answer: I'm Naveen. It's nice to meet you.
在上面的示例中,request 输出
{
"id": "cmpl-77BD86ixAzutOIdaOP6nNY98V5vSf",
"object": "text_completion",
"created": 1681945746,
"model": "text-davinci-003",
"choices": [
{
"text": "\n\nI'm Naveen. It's nice to meet you.",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 6,
"completion_tokens": 14,
"total_tokens": 20
}
}