【问题标题】:How to filter with the WordPress shortcode API in combination with coinmarketcap API如何结合使用 WordPress 短代码 API 和 coinmarketcap API 进行过滤
【发布时间】:2018-02-28 21:28:28
【问题描述】:

所以我正在使用 coinmarketcap api 并尝试将它与 WordPress 结合使用。

在 WordPress 中,我使用以下 php 代码:

function api() {
$url = 'https://api.coinmarketcap.com/v1/ticker/?start=0&limit=250';
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
$name = $api_response[1]["name"];
$usd = $api_response[1]["price_usd"];
echo $name . "<br />";
echo $usd;
}
add_shortcode( 'api_short', 'api' );

所以这段代码是有效的,我通过使用简码 api_short 在 WordPress 页面上得到我的结果。

我现在的问题是我想通过以下方式使用简码:

[api 短名称 ="20"]

通过这种方式,我可以轻松地从数据切换,只需使用简码,而不是一直更改代码。

在这种情况下,“名称”变量是加密货币的名称,如下所示:https://api.coinmarketcap.com/v1/ticker/?start=0&limit=250。 0 = 比特币,1 = 以太坊。

我希望有人知道如何让这个工作,我尝试了一些东西,但到目前为止还没有结果。

可以在此处找到 WordPress 简码文档:https://codex.wordpress.org/Shortcode_API

【问题讨论】:

    标签: php wordpress api


    【解决方案1】:

    一些事情:

    第一:

    为您的函数添加前缀 - 特别是如果您使用的名称具有全局范围或吸引力。

    api() 确实应该类似于solaiman_api() 以防止任何冲突。

    第二:

    您应该使用WP Transients API 来缓存结果,这样您就不会限制 CMC API 的速率

    第三:

    对于您的问题的实际答案,您只需将 URL 中的输入值参数化即可。

    function solaiman_api( $atts ) {
        extract( shortcode_atts( array(
            'placeholder' => ''
        ), $atts ) );
    
        $coin  = $atts['coin'] ? $atts['coin'] : 0; // Default to 0 for BTC
        $limit = $atts['limit'] ? $atts['limit'] : 250; // Default to 250
    
        $url = 'https://api.coinmarketcap.com/v1/ticker/?start='. $coin .'&limit='. $limit;
    
        $response     = wp_remote_get( esc_url_raw( $url ) );
        $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
    
        foreach( $api_response as $c ){
            echo $c['name'].': $'.$c['price_usd'].'<br />';
        }
    }
    add_shortcode( 'cmc_api', 'solaiman_api' );
    

    请注意,我更改了您的函数名称和简码,因为 api_short 的描述性不强。

    这个短代码现在看起来像这样:

    [cmc_api coin="1" limit="1"]
    

    这应该给你以太坊的价格。

    改用此代码:

    这里有一个更好的,根据 CMC 的服务条款将结果缓存 10 分钟:

    function solaiman_api( $atts ) {
        extract( shortcode_atts( array(
            'placeholder' => ''
        ), $atts ) );
    
        $coin  = $atts['coin'] ? $atts['coin'] : 0; // Default to 0 for BTC
        $limit = $atts['limit'] ? $atts['limit'] : 250; // Default to 250
    
        $url = 'https://api.coinmarketcap.com/v1/ticker/?start='. $coin .'&limit='. $limit;
    
        $transient_name = 'cmc_api_'.$coin.'_'.$limit; // One transient per coin per limit
    
        if( false === ( $response = get_transient( $transient_name ) ) ){
            $response = wp_remote_get( esc_url_raw( $url ) );
            set_transient( $transient_name, $response, 600 ); // Cache for 10 minutes
        }
    
        $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
    
        foreach( $api_response as $c ){
            echo $c['name'].': $'.$c['price_usd'].'<br />';
        }
    }
    add_shortcode( 'cmc_api', 'solaiman_api' );
    

    您可以在此处查看[cmc_api coin="1" limit="1"] 的示例:http://xhynk.com/headless/2018/02/28/cmc-test/

    【讨论】:

    • 感谢您的解释和快速回复!非常感谢!对这一切还是有点陌生​​,但很高兴像你这样的人在这里提供帮助:)
    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    相关资源
    最近更新 更多