
wp_remote_get 发送 GET 请求到 URL 并返回数据
函数功能
使用GET方法执行HTTP请求并返回其响应。
函数参数
参数 | 数据类型 | 是否必需 | 描述 | 默认值 |
---|---|---|---|---|
$url | 字符串 | 是 | 请求 URL | 无 |
$args | 数组 | 否 | 请求参数 | array() |
函数返回值
成功时,返回一个数组,失败时,返回一个 WP_Error 对象
使用示例
发送 HTTP 请求时,我们可以通过该函数的 $args 参数来设置 HTTP 的各种参数,包括超时时间,HTTP 版本,Headers 等信息。
请求时设置超时时间和 HTTP 版本。
1 2 3 4 5 6 | $response = wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1', ) ); |
请求时设置内容类型和API Key。
1 2 3 4 5 6 7 8 | $args = array( 'headers' => array( 'Content-Type' => 'application/json', 'X-Api-Key' => 'apikey12345' ) ) $response = wp_remote_get( $url, $args ); |
相关函数
wp_remote_post