WordPress函数:wp_remote_retrieve_body 从原始数据中获取body

江河/ 2023年10月10日/ WordPress/ 浏览 473

函数原型:


wp_remote_retrieve_body( array|WP_Error $response ): string


从原始响应中获取正文-body。响应的正文。如果未给定正文或参数不正确,则为空字符串。


函数源码:


function wp_remote_retrieve_body( $response ) {
	if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
		return '';
	}

	return $response['body'];
}


使用举例:


$url      = 'http://example.org/api';
$response = wp_remote_get( esc_url_raw( $url ) );

/* Will result in $api_response being an array of data,
parsed from the JSON response of the API listed above */
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );


发表评论

暂无评论,抢个沙发...

客服 工单