path = $path; } /** * 发送请求 * * @since Beginning 4.0.5 */ public function request() { if ( !$ssl_rule = $this->get_ssl_rule() ) return false; $args = $this->get_http_args(); if ( in_array( $ssl_rule, array( 'force', 'try' ) ) ) { $url = $this->get_url( 'https' ); $response = wp_safe_remote_request( $url, $args ); if ( $ssl_rule == 'force' && is_wp_error( $response ) ) return false; } if ( !isset( $response ) || is_wp_error( $response ) ) { $url = $this->get_url(); $response = wp_safe_remote_request( $url, $args ); } if ( wp_remote_retrieve_response_code( $response ) != 200 ) return false; $this->body = wp_remote_retrieve_body( $response ); return true; } /** * 获取内容 * * @since Beginning 4.1.0 */ public function get_body() { return $this->body; } /** * 解析 JSON 数据 * * @since Beginning 4.0.5 */ public function parse_json() { $body = trim( $this->body ); $results = json_decode( $body, true ); if ( $success = is_array( $results ) ) $this->results = $results; return $success; } /** * 获取结果 * * @since Beginning 4.0.5 */ public function get_results() { return $this->results; } /** * 获取指定路径的 API 服务器网址 * * @since Beginning 4.0.5 */ private function get_url( $scheme = 'http' ) { $domain = $this->get_domain( $scheme ); return $domain . '/' . ltrim( $this->path, '/' ); } /** * 获取指定协议的 API 服务器域名 * * @since Beginning 4.0.5 */ private function get_domain( $scheme = 'http' ) { $domain = '//' . $this->domain; return set_url_scheme( $domain, $scheme ); } /** * 根据服务器环境获取 SSL 请求规则 * * @since Beginning 4.0.5 */ private function get_ssl_rule() { if ( !wp_http_supports( array( 'ssl' ) ) ) { if ( $this->ssl_rule == 'force' ) return false; if ( $this->ssl_rule == 'try' ) return 'disable'; } return $this->ssl_rule; } /** * 获取 HTTP 请求参数 * * @since Beginning 4.0.5 */ private function get_http_args() { $timeout = defined( 'DOING_CRON' ) && DOING_CRON ? 30 : 5; $timeout += round( strlen( maybe_serialize( $this->data ) ) / 2000 ); // @TODO Headers $args = array( 'user-agent' => sprintf( 'WordPress/%s; %s', get_bloginfo( 'version' ), home_url() ), 'timeout' => $timeout, 'method' => $this->method, 'body' => $this->data ); return $args; } } // End of page.