gpt4 book ai didi

wordpress - 如何将第三方 API 结果插入 WordPress 页面/内容?

转载 作者:行者123 更新时间:2023-12-02 22:18:16 25 4
gpt4 key购买 nike

我有一个 CURL 方法,用于通过 GitBash 调用第三方 API:

curl -u "xxx@dxwxcxrp.com:hPhO4IXXXXXYM2zXXXXX" -X GET -H "Content-Type: application/json" "https://dxwxcxrp.atlassian.net/rest/api/3/search?jql=created>=startOfMonth()&key=DCHC01&startAt=1&maxResults=1&fields=id,key"

结果将是这样的:

{
"expand":"names,schema",
"startAt":1,
"maxResults":1,
"total":31,
"issues":[
{
"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id":"28648",
"self":"https://dxwxcxrp.atlassian.net/rest/api/3/issue/28648",
"key":"DCHC01-7059"
}
]
}

我们如何在 WordPress 网站上显示它?我们需要为此创建一个插件吗?或者还有其他简单实现的方法吗?

最佳答案

取决于您想要在哪里显示它,以及(有时)您的网站是如何构建的。您可以使用 shortcodes ,这只需要 functions.php 中的一个函数(并注册回调),或者您可以构建一个小插件。如果您希望它在每个页面加载时运行并且您不担心 API 速率限制,那么短代码解决方案可能是最容易实现的。

对上面链接的示例进行修改:

function startOfMonth()
{
return 1;
}

// [dcpartners_json]
function dcpartners_json($atts)
{
$ch = curl_init();
$startOfMonth = startOfMonth();
$auth = "xxx@dxwxcxrp.com:hPhO4IXXXXXYM2zXXXXX";

curl_setopt($ch, CURLOPT_URL, "https://dxwxcxrp.atlassian.net/rest/api/3/search?jql=created>=$startOfMonth&key=DCHC01&startAt=1&maxResults=1&fields=id,key");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
curl_setopt($ch, CURLOPT_USERPWD, $auth);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$json = curl_exec($ch);
curl_close();

return $json;
}

add_shortcode('dcpartners_json', 'dcpartners_json');

这只会输出 JSON 字符串,而不是 HTML。

关于wordpress - 如何将第三方 API 结果插入 WordPress 页面/内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com