首页 小编推荐 正文
  • 本文约2359字,阅读需12分钟
  • 105380
  • 44

wordpress调用api接口

温馨提示:本文最后更新于2025年10月6日 00:30,若内容或图片失效,请在下方留言或联系博主。
摘要

在 WordPress 中调用 API 接口,通常可以通过以下几种方式实现:

  1. 使用 wp_remote_get() 函数
    wp_remote_get() 是 WordPress 提供的用于发起 GET 请求的函数。示例如下:

    
    $respo...

在 WordPress 中调用 API 接口,通常可以通过以下几种方式实现:

  1. 使用 wp_remote_get() 函数
    wp_remote_get() 是 WordPress 提供的用于发起 GET 请求的函数。示例如下:

    $response = wp_remote_get( 'https://api.example.com/data' );
    
    if ( is_wp_error( $response ) ) {
       // 处理错误
       $error_message = $response->get_error_message();
       echo "请求失败: $error_message";
    } else {
       $body = wp_remote_retrieve_body( $response );
       $data = json_decode( $body, true );
       print_r( $data );
    }
  2. 使用 wp_remote_post() 函数
    如果需要发送 POST 请求,可以使用 wp_remote_post() 函数:

    $args = array(
       'method' => 'POST',
       'headers' => array(
           'Content-Type' => 'application/json'
       ),
       'body' => json_encode( array(
           'username' => 'test',
           'password' => '123456'
       ) )
    );
    
    $response = wp_remote_post( 'https://api.example.com/login', $args );
    
    if ( is_wp_error( $response ) ) {
       // 错误处理
    } else {
       $body = wp_remote_retrieve_body( $response );
       $data = json_decode( $body, true );
       print_r( $data );
    }
  3. 使用 WP_Http 类(更灵活)
    WP_Http 是 WordPress 的底层 HTTP 请求类,适合更复杂的场景:

    $http = new WP_Http();
    $response = $http->request( 'https://api.example.com/data', array( 'method' => 'GET' ) );
    
    if ( is_wp_error( $response ) ) {
       // 错误处理
    } else {
       $body = $response['body'];
       $data = json_decode( $body, true );
       print_r( $data );
    }
  4. 在 WordPress 插件或主题中调用 API
    在插件或主题的 PHP 文件中可以直接使用上述方法调用 API。例如,在 functions.php 中添加代码:

    function my_custom_api_call() {
       $response = wp_remote_get( 'https://api.example.com/data' );
    
       if ( ! is_wp_error( $response ) ) {
           $body = wp_remote_retrieve_body( $response );
           return json_decode( $body, true );
       }
    
       return false;
    }
    
    add_action( 'init', 'my_custom_api_call' );
  5. 使用 AJAX 调用 API(前端)
    在前端 JavaScript 中使用 fetch 或 jQuery.ajax() 调用 API,同时通过 WordPress 的 admin-ajax.php 进行安全处理:

    jQuery.ajax({
       url: '/wp-admin/admin-ajax.php',
       type: 'POST',
       data: {
           action: 'my_ajax_handler',
           api_url: 'https://api.example.com/data'
       },
       success: function(response) {
           console.log(response);
       }
    });

    在 PHP 中处理:

    function my_ajax_handler() {
       $url = $_POST['api_url'];
       $response = wp_remote_get( $url );
    
       if ( is_wp_error( $response ) ) {
           wp_send_json_error( 'API 请求失败' );
       } else {
           wp_send_json_success( wp_remote_retrieve_body( $response ) );
       }
    }
    
    add_action( 'wp_ajax_my_ajax_handler', 'my_ajax_handler' );
    add_action( 'wp_ajax_nopriv_my_ajax_handler', 'my_ajax_handler' );
  6. 设置超时和代理(可选)
    有些 API 需要设置超时时间或代理,可以在请求参数中添加:

    $args = array(
       'timeout' => 30,
       'sslverify' => false,
       'blocking' => true
    );
    
    $response = wp_remote_get( 'https://api.example.com/data', $args );
  7. 处理 JSON 响应
    多数 API 返回的是 JSON 格式数据,使用 json_decode() 可以将其转换为 PHP 数组或对象。

  8. 错误处理
    使用 is_wp_error() 判断请求是否出错,并使用 $response->get_error_message() 获取具体错误信息。

  9. 使用缓存减少请求次数(可选)
    对于频繁访问的 API,可以使用 WordPress 缓存机制(如 wp_cache_set() 和 wp_cache_get())来存储结果。

  10. 安全建议

    • 不要在前端暴露敏感 API 密钥。
    • 使用 HTTPS 协议保证通信安全。
    • 设置合理的请求超时时间。
    • 对 API 响应进行验证和过滤,防止注入攻击。
48845 人点赞
评论