如何在 wordpress 中使用 API

How to use API in wordpress

我想用 WordPress 连接一个 Razorpay 支付 API,API 有使用用户名和密码的身份验证令牌。

WordPress 中是否有任何内置功能可用于发出呼叫和处理响应?

你可以使用 wp_remote_get()

例如

wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) );

您还可以控制所有请求参数,例如 headers 和 body 数据。

默认用法

global $wp_version;
$args = array(
    'timeout'     => 5,
    'redirection' => 5,
    'httpversion' => '1.0',
    'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
    'blocking'    => true,
    'headers'     => array(),
    'cookies'     => array(),
    'body'        => null,
    'compress'    => false,
    'decompress'  => true,
    'sslverify'   => true,
    'stream'      => false,
    'filename'    => null
); 

参考:More info