我有一个CURL方法,用于通过GitBash调用第三方API:
curl -u "[email protected]: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 = "[email protected]: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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句