1

我正在尝试创建一个短代码以连接到 API,但该短代码存在问题。我知道这是因为函数在函数内部,但我不知道如何修复。我尝试了一些不起作用的东西。

// 添加简码

function api_test() {

function execute_request( $args ) {
    global $base_url;
    $target_url = add_query_arg( $args, $base_url );
    $data       = wp_remote_get( $target_url );
    echo '<pre><code>';
    print_r( $data['body'] );
    echo '<code></pre>';
}   


if ( ! current_user_can( 'manage_options' ) ) die();

// API variables, please override
$base_url    = 'https://website.com';
$email       = 'email@gmail.com';
$product_id  = '1146';
$license_key = '0g96b29x5v27fmfnmbr4hxaflky';
$instance    = '';

$request = ( isset( $_GET['request'] ) ) ? $_GET['request'] : '';

$links = array(
'check'        => 'Check request',
'activation'   => 'Activation request',
'deactivation' => 'Deactivation',
'version_check'      => 'Version Check',
);

foreach ( $links as $key => $value ) {
echo '<a href="' . add_query_arg( 'request', $key ) . '">' . $value . '</a> | ';
}

// Valid check request
if ( $request == 'check' ) {
$args = array(
    'wc-api'     => 'serial-numbers-api',
    'request'    => 'check',
    'email'      => $email,
    'serial_key' => $license_key,
    'product_id' => $product_id
);
echo '<br>';
echo '<br>';
echo '<b>Valid check request:</b><br />';
//execute_request( $args );
$this->execute_request($args);
}

}

add_shortcode( 'api-test', 'api_test' );
4

1 回答 1

0

只需execute_request()在短代码之外创建。

function execute_request( $args ) {
    global $base_url;
    $target_url = add_query_arg( $args, $base_url );
    $data       = wp_remote_get( $target_url );
    echo '<pre><code>';
    print_r( $data['body'] );
    echo '<code></pre>';
} 

function api_test() {

    if ( ! current_user_can( 'manage_options' ) ) die();

    // API variables, please override
    $base_url    = 'https://website.com';
    $email       = 'email@gmail.com';
    $product_id  = '1146';
    $license_key = '0g96b29x5v27fmfnmbr4hxaflky';
    $instance    = '';

    $request = ( isset( $_GET['request'] ) ) ? $_GET['request'] : '';

    $links = array(
        'check'         => 'Check request',
        'activation'    => 'Activation request',
        'deactivation'  => 'Deactivation',
        'version_check' => 'Version Check',
    );

    foreach ( $links as $key => $value ) {
        echo '<a href="' . add_query_arg( 'request', $key ) . '">' . $value . '</a> | ';
    }

    // Valid check request
    if ( $request == 'check' ) {

        $args = array(
            'wc-api'     => 'serial-numbers-api',
            'request'    => 'check',
            'email'      => $email,
            'serial_key' => $license_key,
            'product_id' => $product_id
        );

        echo '<br>';
        echo '<br>';
        echo '<b>Valid check request:</b><br />';
        //execute_request( $args );
        execute_request($args);
    }

}

add_shortcode( 'api-test', 'api_test' );
于 2021-05-14T12:16:19.730 回答