发起 HTTP 请求,是再平常不过的需求了。一般的就是使用 file_get_contents 或者 cURL。
但是在WordPress中,使用 file_get_contents 或者 cURL 开发的主题或插件,都会被WordPress官方拒绝。因为,WordPress官方已经提供了封装好的 HTTP 请求函数。之前也曾简单介绍过:WordPress 使用wp_remote_get和wp_remote_post 替代curl。
本文先介绍专门用于 HTTP GET 请求的 wp_remote_get 。
函数源码:
function wp_remote_get( $url, $args = array() ) {
$http = _wp_http_get_object();
return $http->get( $url, $args );
}
使用举例:
$response = wp_remote_get( 'https://www.zhuige.com' );
if ( is_array( $response ) && ! is_wp_error( $response ) && $response['response']['code'] == '200' ) {
$headers = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
}
在获取到 $body 后,要根据实际情况,对其进行解析。
-
WordPress函数:is_rtl 确定当前区域设置是否为从右到左(RTL)WordPress函数:is_rtl 确定当前区域设置是否为从右到左(RTL)
-
WordPress函数:is_multisite 是否开启了多站点WordPress函数:is_multisite 是否开启了多站点
-
WordPress函数:is_main_site 是否是主站点WordPress函数:is_main_site 是否是主站点
-
WordPress函数:is_child_theme 是否正在使用子主题WordPress函数:is_child_theme 是否正在使用子主题
-
WordPress函数:current_theme_supports 检查主题对给定功能的支持WordPress函数:current_theme_supports 检查主题对给定功能的支持
-
WordPress函数:is_customize_preview 是否在自定义设置程序中预览网站WordPress函数:is_customize_preview 是否在自定义设置程序中预览网站
暂无评论,抢个沙发...