函數(shù)功能
使用GET方法執(zhí)行HTTP請求并返回其響應。
函數(shù)參數(shù)
| 參數(shù) | 數(shù)據(jù)類型 | 是否必需 | 描述 | 默認值 |
|---|---|---|---|---|
| $url | 字符串 | 是 | 請求 URL | 無 |
| $args | 數(shù)組 | 否 | 請求參數(shù) | array() |
函數(shù)返回值
成功時,返回一個數(shù)組,失敗時,返回一個 WP_Error 對象
使用示例
發(fā)送 HTTP 請求時,我們可以通過該函數(shù)的 $args 參數(shù)來設置 HTTP 的各種參數(shù),包括超時時間,HTTP 版本,Headers 等信息。
請求時設置超時時間和 HTTP 版本。
$response = wp_remote_get( 'http://www.example.com/index.php?action=foo',
array(
'timeout' => 120,
'httpversion' => '1.1',
)
);
請求時設置內(nèi)容類型和API Key。
$args = array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Api-Key' => 'apikey12345'
)
)
$response = wp_remote_get( $url, $args );