_fetch_remote_file

函数
_fetch_remote_file ( $url, $headers = "" )
参数
  • (string) $url URL to retrieve
    Required:
  • (array) $headers Optional. Headers to send to the URL.
    Required:
    Default: (empty)
返回值
  • (Snoopy) style response
定义位置
相关方法
wp_safe_remote_getremove_filter_deprecated_filewp_safe_remote_headkses_remove_filters
引入
1.5.0
弃用
-

fetch_remote_file: 这个函数用来下载一个远程文件并保存到服务器上的一个本地文件。

使用WP HTTP Request API检索URL头和内容。

function _fetch_remote_file($url, $headers = "" ) {
	$resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) );
	if ( is_wp_error($resp) ) {
		$error = array_shift($resp->errors);

		$resp = new stdClass;
		$resp->status = 500;
		$resp->response_code = 500;
		$resp->error = $error[0] . "n"; //n = Snoopy compatibility
		return $resp;
	}

	// Snoopy returns headers unprocessed.
	// Also note, WP_HTTP lowercases all keys, Snoopy did not.
	$return_headers = array();
	foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
		if ( !is_array($value) ) {
			$return_headers[] = "$key: $value";
		} else {
			foreach ( $value as $v )
				$return_headers[] = "$key: $v";
		}
	}

	$response = new stdClass;
	$response->status = wp_remote_retrieve_response_code( $resp );
	$response->response_code = wp_remote_retrieve_response_code( $resp );
	$response->headers = $return_headers;
	$response->results = wp_remote_retrieve_body( $resp );

	return $response;
}

常见问题

FAQs
查看更多 >