wp_remote_get sends a GET request to a URL and returns data

Posted in:
Update time:2020-10-27

function function

使用GET方法执行HTTP请求并返回其响应。

function parameter

parametersdata typeRequired or notdescriptivedefault value
$urlstring (computer science)beRequest URLnot have
$argsarraysclogged请求参数array()

Function Return Value

成功时,返回一个数组,失败时,返回一个 WP_Error 对象

usage example

发送 HTTP 请求时,我们可以通过该函数的 $args 参数来设置 HTTP 的各种参数,包括超时时间,HTTP 版本,Headers 等信息。

请求时设置超时时间和 HTTP 版本。

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

请求时设置内容类型和API Key。

$args = array(
    'headers' => array(
        'Content-Type' => 'application/json',
        'X-Api-Key' => 'apikey12345'
    )
)

$response = wp_remote_get( $url, $args );

related function

wp_remote_post

We offer WordPress Themes and Plugins Custom Development Services

This site has long undertaken WordPress themes, plugins, WooCommerce-based store mall development business. We have 10 years of experience in WordPress development, if you want to Developing Websites with WordPress, please contact WeChat: iwillhappy1314 or email: amos@wpcio.com for inquiries.

发表回复

Your email address will not be published. 必填项已用 * 标注

*