url_is_accessable_via_ssl()
云策文档标注
概述
url_is_accessable_via_ssl() 函数用于通过 WordPress HTTP API 测试 URL 是否可通过 HTTPS 访问。该函数已在 WordPress 4.0.0 版本中被弃用。
关键要点
- 函数功能:检查指定 URL 是否支持 SSL 访问,通过将 URL 方案设置为 https 并发送 HTTP GET 请求来实现。
- 参数:接受一个必需的字符串参数 $url,表示要测试的 URL。
- 返回值:返回布尔值,true 表示 SSL 访问可用(响应状态码为 200 或 401),false 表示不可用或发生错误。
- 弃用状态:自 WordPress 4.0.0 起被弃用,建议使用替代方法。
- 依赖函数:内部使用 set_url_scheme()、wp_remote_get()、wp_remote_retrieve_response_code()、_deprecated_function() 和 is_wp_error() 等核心函数。
代码示例
function url_is_accessable_via_ssl( $url ) {
_deprecated_function( __FUNCTION__, '4.0.0' );
$response = wp_remote_get( set_url_scheme( $url, 'https' ) );
if ( !is_wp_error( $response ) ) {
$status = wp_remote_retrieve_response_code( $response );
if ( 200 == $status || 401 == $status ) {
return true;
}
}
return false;
}注意事项
- 该函数已被弃用,在开发新代码时应避免使用,并考虑使用更现代的 HTTP 请求方法或自定义实现。
- 函数内部处理错误:如果 wp_remote_get() 返回 WP_Error 对象,则直接返回 false。
- 状态码判断:仅当响应状态码为 200(成功)或 401(未授权)时返回 true,其他情况均返回 false。
原文内容
Determines if the URL can be accessed over SSL.
Description
Determines if the URL can be accessed over SSL by using the WordPress HTTP API to access the URL using https as the scheme.
Parameters
$urlstringrequired-
The URL to test.
Source
function url_is_accessable_via_ssl( $url ) {
_deprecated_function( __FUNCTION__, '4.0.0' );
$response = wp_remote_get( set_url_scheme( $url, 'https' ) );
if ( !is_wp_error( $response ) ) {
$status = wp_remote_retrieve_response_code( $response );
if ( 200 == $status || 401 == $status ) {
return true;
}
}
return false;
}